Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a try catch code block automatic in Android Studio by shortcut?

In Eclipse, when I enter code Thread.sleep(100), the code will be wrappered by try catch after entering keys: ctrl+1 and enter. In Android studio what can I do for wrapper it by try catch easily? Is there a shortcut?

like image 280
aolphn Avatar asked Jun 06 '16 08:06

aolphn


People also ask

How do I change hotkeys in Android Studio?

To open the keymap settings, choose File > Settings (on Mac, Android Studio > Preferences) and navigate to the Keymap pane. Figure 1. The Android Studio keymap settings window. Keymaps dropdown: Select the desired keymap from this menu to switch between preset keymaps.

How do I get the try catch block in eclipse?

In Eclipse, just surround the code you want to enclose with the try-catch block and right-click, then select **Surround with > Try Catch **block. Although you can only have one try statement per try-catch block, you can have multiple catch blocks.

Can I use try catch instead of if else?

Try/catch is used when an Exception can be thrown from the code, and you catch it in the catch-clause, which is an object oriented way to handle errors. You can't catch Exceptions with if/else blocks - they share nothing with try/catch.


1 Answers

Highlight the code you want to surround with try/catch

For Windows it is:

Press Ctrl + Alt + t

For Mac it is:

Press cmd + Alt + t

Select the item you want (#7 for try / catch)

There is no need to "type" try first however, highlighting your code works better.

like image 198
Snappy Cracker Avatar answered Oct 06 '22 17:10

Snappy Cracker