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

Intellij Idea

Intellij Idea Problem Overview


Can I select a block of code and have IntelliJ wrap it with a "try...catch" ?

Is there a keyboard shortcut?

Intellij Idea Solutions


Solution 1 - Intellij Idea

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.

Solution 2 - Intellij Idea

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

Solution 3 - Intellij Idea

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"

Solution 4 - Intellij Idea

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)
  1. 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.

Solution 5 - Intellij Idea

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

Solution 6 - Intellij Idea

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. enter image description here

Solution 7 - Intellij Idea

For beloved Mac users it's: Command Alt T

Solution 8 - Intellij Idea

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

Solution 9 - Intellij Idea

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - Intellij IdeaduffymoView Answer on Stackoverflow
Solution 2 - Intellij Ideauser2305886View Answer on Stackoverflow
Solution 3 - Intellij IdeaAndrewView Answer on Stackoverflow
Solution 4 - Intellij IdeamsysmiluView Answer on Stackoverflow
Solution 5 - Intellij IdeaJeeterView Answer on Stackoverflow
Solution 6 - Intellij IdeaHardik RanaView Answer on Stackoverflow
Solution 7 - Intellij IdeaKarim VarelaView Answer on Stackoverflow
Solution 8 - Intellij IdeaMenuka IshanView Answer on Stackoverflow
Solution 9 - Intellij IdealocalhostView Answer on Stackoverflow