开发者

"Wrap with try...catch" in IntelliJ?

Can I select a 开发者_开发知识库block of code and have IntelliJ wrap it with a "try...catch" ?

Is there a keyboard shortcut?


Select the code, and then either:

  • Choose Code > Surround With
  • Press Ctrl-Alt-T. (Command-Option-T for OS X.)

I like to check the Productivity Guide under the Help menu from time to time. Not only does it tell me all the shortcuts, but it keeps track of how many times I've used each one and when I last used it. I can see how well I'm leveraging the shortcuts.


Ctrl Alt T brings a new terminal window in Linux systems. So right approach is Menu->Code and select Surround with...


Ubuntu:

"alt+c" -> "s" -> "6"

Mac:

"cmd+alt+t" -> "6"

Win (also Linux distrs if no conflict with system key binding):

"ctrl+alt+t" -> "6"


To recap and overview all situations, one might do:

  1. Indicate which portion of code to surround:

    • Keep the pointer on the line you wish to surround or
    • Select the the whole lines you wish to surround (can't surround partial lines)
  2. Command:

    • Win: Ctrl-Alt-T, 6 or Alt+C, S, 6
    • Mac: Command+Alt+T, 6
    • Linux: Alt+C, S, 6

Lastly, don't forget to use the Productivity Guide under the Help menu.


If you only need to wrap one line in a try-catch, you can use the Postfix Completion, available in IDEA 13.1 EAP and above.

The idea is that all you need to do is append .try to your line and the completion will be able to wrap it in a try-catch.

For example:

System.out.println("Hello World!");.try

results in

try {
    System.out.println("Hello World!");
} catch (Exception e) {
    e.printStackTrace();
}


Select the code block to surround and then press Ctrl + Alt + T (or right-click the selection and select Surround with… from the menu). IntelliJ IDEA will show a list of options to choose from.

"Wrap with try...catch" in IntelliJ?


Intellij Idea provide surround with functionality for that. below link you can see what you can achieve with surround with https://www.jetbrains.com/help/idea/2016.2/surrounding-blocks-of-code-with-language-constructs.html

If you are using Ubuntu and already read above answers you may see that default key shortcut for surround with Ctrl+Alt+T is open terminal in Ubuntu.
So one way to use surround with is, in Menu Code -> Surround with. But this not let us use Intellij core promise of only keyboard development. To keep the promise Intellij let us to use our own keyboard shortcuts.

For That go to File -> Setting -> Keymap in there you you can see a search box. Type the functionality you want (Surround with). Right click on Surround with, then select Remove Ctrl+Alt+T. Then again right click and select Add Keyboard Shortcut The Give keystroke you need (ex: press Ctrl + Alt + P) Then press Ok. It might prompt an error saying the keystroke in use, If you want it press Leave. Then press Apply

If you want to know default key strokes in Intellij Idea, check this pdf provided by Jetbrains

[Updated Link]
https://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf


For beloved Mac users it's: Command Alt T


select the code and press alt+shift+z..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜