Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What does Ctrl Shift F do in IntelliJ?

In past IntelliJ versions, ctrl+shift+f would search the entire project (no matter whether you had at some point used "find in path").

How do I enable Force shortcuts in IntelliJ?

Add a keyboard shortcutOn the Keymap page of the Settings/Preferences dialog Ctrl+Alt+S , right-click an action and select Add Keyboard Shortcut. In the Keyboard Shortcut dialog, press the necessary key combination.


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();
}