Java Exception Handling

An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons. Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

To understand how exception handling works in Java, you need to understand the three categories of exceptions:

Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Go through these tutorials to gain more insight.

Related Tags

Tutorials

exceptions-notes

Selenide IllegalArgumentException: Failed to create folders

The error “java.lang.IllegalArgumentException: Failed to create folder” in Selenide typically happens when Selenide tries to save screenshots, page sources, or logs, but it cannot create the required directories due to permission issues or incorrect paths.

Java throw and throws Keywords

In Java exception handling, throw keyword is used to explicitly throw an exception from a method or constructor. And throws keyword is used to declare the list of exceptions that may be thrown by that method or constructor. 1. Throw Let us learn basic things about throw keyword before going …

Java try catch finally

Java try catch finally blocks helps in writing the application code which may throw exceptions in runtime and gives us chance to recover from the exception.

Java Checked vs Unchecked Exceptions

Learn the difference between checked vs unchecked exceptions in Java, with simple explanations and examples. Learn Java exception handling best practices.

[Solved] java.lang.IncompatibleClassChangeError: Implementing class

Reason When you are working with java libraries that internally depend on cglib, then it’s very normal to encounter the below error: This error is due to an incompatible version of cglib with additional jar files in your project, such as asm. Solution The solution is very easy. Just place …

Java NullPointerException

Java NullPointerException (NPE) is an unchecked exception and extends RuntimeException. NullPointerException doesn’t force us to use a try-catch block to handle it. NullPointerException has been very much a nightmare for most Java developers. It usually pop up when we least expect them. I have also spent a lot of time …

Java Suppressed Exceptions with Examples

Suppressed exceptions, as name suggest, are exceptions thrown in the code but were ignored somehow. If you remember try-catch-finally block execution sequence and how they return any value or exceptions, you will recall that exceptions thrown in finally block are suppressed if an exception is thrown in try block also. …

Java Synchronous and Asynchronous Exceptions

In this Java tutorial, learn about asynchronous and synchronous exceptions in Java. Also, learn how they are different with checked and unchecked exceptions. 1. Asynchronous and synchronous exceptions Normally Java differentiates the exceptions into two categories on basis of “timing” when they are discovered. These categories are checked and unchecked …

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.