Skip to main content

Android Studio Ninja Tip: Custom try/catch code template

As an Android Studio user, you should be familiar with the “surround-with try-catch” functionality.

Surround with try-catch

When you use Ctrl+Alt+T(Linux) / Cmd+Alt+T(Mac) shortcut, the built-in try-catch generator auto generates a call to the printStackTrace() method of class Exception in the catch block. After this, we manually add some code to log the exception.
e.g.,
Log.e(TAG, “Oops! something gone wrong”, e);

As a power user, you may have used some or all of the following built-in Live templates:

Live Templates


You can save yourself the pain of these 5-keystrokes or repetitive typing or copy pasting the code by becoming a Ninja who uses a custom built-in try-catch template.


Steps for customization

Step-1: Go to Android Studio Preferences | Editor | File and Code Templates
Step-2: Select Code | Catch Statement Body
Step-3: Replace the existing template from:
${EXCEPTION}.printStackTrace();
with:
Log.e(getClass().getSimpleName(), "Exception logged", ${EXCEPTION});

I generally use the following template, which logs the error using Timber and records the exception on Crashlytics.

Timber.e(${EXCEPTION}, "Failed with %s", ${EXCEPTION}.getClass().getSimpleName());
Crashlytics.log(${EXCEPTION}.getMessage());

Sample Preview


Preview showing  the usage customized try-catch template

Comments

Popular posts from this blog

Android : Sharing code and resources between UI and unit tests

As an Android developer you are already familiar with writing Unit and UI tests. This post is about "How to share the code and resources between your UI and unit tests?". I'd used "Appointments List" as an example scenario for this post. Existing Setup For UI testing, I'd have list of appointments API response in JSON format as "appointments.json" in res directory. The .json file is is converted to AppointmentsListResponse using JsonParser. androidTest/java/com/example/AppointmentScreenTest.java AppointmentsListResponse getFakeAppointmentsResponse() { InputStream in = getInstrumentation ().getContext() .getResources() .openRawResource(com.example.android.internal.test.R.raw.appointments); AppointmentsListResponse fakeResponse = new JsonParser(in).parseTo(AppointmentsListResponse. class ); } androidTest/ java/ com/example/JsonParser.java public class JsonParser String mStringToParse; private static Gson mGso...

How to enable Scrolling in Opensuse11.4 ?

      I have recently installed Opensuse 11.4 KDE in Dell Inspiron N5010, the problem with it is Opensuse cannot support scrolling. Step 1  To make Scroll button Work install GSynaptics for OpenSuse.  GSynaptics is a GUI configuration tool for the Synaptics touchpad  driver of the X server. ravi@linux-2ezc:~>sudo zypper in gsyanptics Step 2         Edit the xorg.conf ravi@linux-2ezc:~> kdesu kwrite /etc/X11/xorg.conf.d/50-synaptics.conf Section "InputDevice" Driver "synaptics" Identifier "TouchPad" Option "SendCoreEvents" Option "Protocol" "auto-dev" Option "SHMConfig" "on" EndSection Section "InputClass" Identifier "touchpad catchall" Driver "synaptics" MatchIsTouchpad "on" MatchDevicePath "/dev/input/event*" Option "TapButton1" "...