Error Handling in Android Studio

10:50

Try-Catch in Java

Errors and Exceptions are conditions that may happen almost in every bit of your code. Being aware of how to handle them plays an important part of how efficient your application will be. Therefore, the goal of this post is to give you an idea of how it all works in Android Studio, and by reading this you would be able to handle the majority of exceptions provided by java programming language.

The most common method used in Android Studio is try-catch. As well as in java programming, that consists of making the node "try" to include the routine that may fall into an error. Then, with the node "catch" you tell the application what to do when catching a specific exception.

Simply like this:
try {
    // routine 1
    // routine 2
    // routine 3
}
catch (Exception e) {
    // Handle the error/exception
}
With this structure, the code will reach the node "catch" as soon as any exception has been caught. If it happens in the routine 2, routine 3 will be then ignored and the code will execute the catch node. Then you can print the message of the exception, or print the stack trace, or even define an alternative routine if necessary.

You can also handle specific exceptions by adding multiple "catch" nodes, and then even throw a new custom error. Note that "throw" can also be used on the fly in other parts of your code.

public void ExampleMethod() throws IOException
{
    if (conditionForError == true)
    {
        throw new IOException("Custom exception msg");
    }

    try
    {
        // routine 1
        // routine 2
        // routine 3
     }
    catch (ExceptionRoutine1 e)
    {
        // That's good for diagnosing the problem
        System.out.println("Thrown exception: " + e.getMessage());
    }
    catch (ExceptionRoutine2 e)
    {
        // custom exception
        throw new Exception("Error in Routine 2", e);
    }
    catch (Exception e)
    {
        // General error can be anything*
        // captured by the java class Exception
        // print in the console detailed technical info
        e.printStackTrace();
    }

Notice that the "general catch" must always be at last in your code.
See how the exception classes are organized in Java.

Exception Hierarchy in Java

It is important to notice that some errors can be expected, for example when reading the user input and computing the data into a variable. Several errors may come up which are predictable, such as:

  • Array out of index exception
  • Input is not in a valid format

However, some errors may appear from the logic or the structure of the code itself. Those can be harder to predict, but should be always take into account while doing your code. No one wants the end user to get an error like:

  • Stack overflow
  • NullerPointException



Java Resources

Android Studio uses all features from Java language in order to provide a good set of tools and methods to handle exceptions. It is crucial for us to identify where an error may come up, and either throw an exception, or catch it with try-catch, so that the application in development would be consistent and respond as expected.

Find more information on the links below, and stay tuned for more posts like this.
- Java With Us, exception hierarchy
- Javamex, throwing exceptions
- Oracle Java Class Exception

You Might Also Like

0 comments

Couch Jumping