Open In App

Exceptions in Android with Example

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Exceptions in Android refer to abnormal events or conditions that occur during the execution of an app, which can cause the app to crash or behave unexpectedly. There are several types of exceptions that can occur in Android, including:

  • Checked Exceptions: These are exceptions that are checked by the compiler at compile-time, and must be handled or declared in the code. Examples of checked exceptions include IOException and SQLException.
  • Unchecked Exceptions: These are exceptions that are not checked by the compiler, and can occur at runtime. Examples of unchecked exceptions include NullPointerException and ArrayIndexOutOfBoundsException.
  • Android-Specific Exceptions: These are exceptions that are specific to the Android operating system, and are typically related to the Android framework. Examples of Android-specific exceptions include ActivityNotFoundException and SecurityException.

When an exception occurs, it is important for the developer to handle it properly to prevent the app from crashing, and to provide a meaningful error message or recovery action to the user. The standard way of handling exceptions in android is through try-catch blocks.

For example:

try {
    // Code that may throw an exception
} catch(IOException e) {
    // Code to handle IOException
} catch(SQLException e) {
    // Code to handle SQLException
} catch(Exception e) {
    // Code to handle any other exception
}

There are other ways to handle exceptions in Android other than try-catch, such as using the Thread.setDefaultUncaughtExceptionHandler() method to handle exceptions that occur in background threads or using the ACRA library to automatically report exceptions to a remote server for further analysis.

Using a try-catch block to handle a checked exception (IOException) in Java and Kotlin

Java




try {
    FileInputStream file = new FileInputStream("file.txt");
    // code that uses the file
} catch (IOException e) {
    // handle the exception
    Log.e("MyApp", "Error reading file", e);
}


Kotlin




try {
    val file = FileInputStream("file.txt")
    // code that uses the file
} catch (e: IOException) {
    // handle the exception
    Log.e("MyApp", "Error reading file", e)
}


Using a try-catch block to handle an unchecked exception (NullPointerException) in Java and Kotlin

Java




try {
    String str = null;
    str.length();
} catch (NullPointerException e) {
    // handle the exception
    Log.e("MyApp", "Error: variable is null", e);
}


Kotlin




try {
    val str: String? = null
    str?.length
} catch (e: NullPointerException) {
    // handle the exception
    Log.e("MyApp", "Error: variable is null", e)
}


Using Thread.setDefaultUncaughtExceptionHandler() in Java and Kotlin

Java




Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable e) {
        // handle the exception
        Log.e("MyApp", "Error: Uncaught exception", e);
    }
});


Kotlin




Thread.setDefaultUncaughtExceptionHandler { thread, e ->
    // handle the exception
    Log.e("MyApp", "Error: Uncaught exception", e)
}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads