Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I customize the code that is generated when I use "Surround with try/catch"?

Each Android developer was stuck with the next situation:

public void catchMeThod() {
    throwedMethod();
}

public void throwedMethod() throws IOException{
    throw new IOException("File is missing.");    
}

Since IOException is Checked exception throwedMethod obliges us to handle it.

When I move the caret inside throwedMethod and press Alt + Enter, Android Studio offers me some possible scenarios:

![enter image description here

By default I choose the Surround with try/catch option and Android Studio generates the next code:

enter image description here

And my question is: how to change this trigger, to replace

e.printStackTrace() 

with

Log.e(getClass().getSimpleName(), "Handled exception", e);
like image 908
Sergey Shustikov Avatar asked Jun 02 '16 09:06

Sergey Shustikov


1 Answers

  1. Goto Android Studio Preferences | Editor | File and Code Templates

  2. Select Code | Catch Statement Body

  3. Replace the existing template from:

    ${EXCEPTION}.printStackTrace(); 
    

    with:

    Log.e(getClass().getSimpleName(), "Exception handled", ${EXCEPTION});
    

Preview:

Custom try..catch template

Enjoy the customized template :)

like image 194
blizzard Avatar answered Oct 26 '22 04:10

blizzard