aolphn
aolphn

Reputation: 2998

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?

Upvotes: 37

Views: 49933

Answers (10)

Khaaki G
Khaaki G

Reputation: 1

type try and press tab button, it will create try/catch template easily

Upvotes: 0

Jayesh Dankhara
Jayesh Dankhara

Reputation: 829

Short Cut of Generating predefined/custom templates

Press Ctrl + Alt + T

Surround with many blocks like

  • try/catch
  • try/catch/finally
  • if
  • if/else
  • {}
  • < Edit fold > Commits

Upvotes: 0

ChrisOdney
ChrisOdney

Reputation: 6384

Select the code you want to surround with try/catch and press Alt + Shift + Z, this will bring up try/catch along with any other applicable templates like this:

Try/Catch option in the resulting menu

To generate a try/catch block type 'try' and press ctrl + space.

The Ctrl + Shift + T shortcut launches the terminal in the eclipse version I am using - 2019-03.

Upvotes: 1

vss
vss

Reputation: 1153

Highlight the necessary code you need to surround with, and press one of the key combinations depending upon your Operating System.

Windows: Ctrl + Alt + T

Linux(Ubuntu): Ctrl + Windows + Alt + T

Mac: Cmd + Alt + T

A list of Surround With options would pop (like in the below image). You can select the appropriate item of your choice either by pressing the respective number of the item on keyboard or manually via mouse click.

Surround With

Upvotes: 16

Snappy Cracker
Snappy Cracker

Reputation: 1481

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.

Upvotes: 84

林果皞
林果皞

Reputation: 7793

In Ubuntu Gnome, Ctrl + Alt + T has been taken already by "Launch terminal", run command gnome-control-center keyboard to check:

enter image description here

Either you disable it by press Back Space OR use Ctrl + Alt + Win + T as alternative.

Keep in mind that if "try / catch" option doesn't shows up, it might caused by warning such as "unexpected token" in your code. You need to fix it first.

Upvotes: 8

Ajeet Yadav
Ajeet Yadav

Reputation: 691

Short Cut of Generating predefined/custom templates

Press Ctrl + Alt + T

Upvotes: 0

Sergey Emeliyanov
Sergey Emeliyanov

Reputation: 6961

On Mac it's: cmd + alt + T. Also for most used shortcuts in Android Studio I'd recommend this link http://www.developerphil.com/android-studio-tips-of-the-day-roundup-2/

Upvotes: 3

ALi Nezamifar
ALi Nezamifar

Reputation: 151

Type "try",
then press alt + ctrl + t,
then select "try / catch".

Upvotes: 8

Harshil Kulkarni
Harshil Kulkarni

Reputation: 411

Try this

  1. Write 'try'
  2. select this word(try)
  3. press Ctrl-Alt-T
  4. Select appropriate one

Upvotes: 7

Related Questions