What is checked exception and unchecked?
In this Java exceptions tutorial, learn what an exception is in Java, and the difference between a checked exception and an unchecked exception. We will also learn some Java exception handling best practices. Show
1. What is Exception in Java?“An exception is an unexpected event that occurred during the execution of a program, and disrupts the normal flow of instructions.”
A few examples of an exception in the program execution can be:
2. Handling a Thrown ExceptionWe have two choices when an exception object is created in our application;
This is a very important decision to be made while setting the responsibilities of a method. A method should clearly indicate what exceptions it will handle and which it will not. It is defined in the method declaration using the To handle the exception, We must catch the exception in catch section of try-catch block.
3. Checked Exception vs Unchecked ExceptionIn Java, exceptions are broadly categorized into two sections:
3.1. Checked ExceptionsThe checked exceptions are those exceptions, as the name suggests, which a method must handle in its body or throw to the caller method so the caller method can handle it. Checked exceptions are checked by the Java compiler, so they are called compile-time exceptions. Java compiler forces us to handle these exceptions in some manner in the application code. We must handle these exceptions at a suitable level inside the application to inform the user about the failure and ask him to retry or come later. Generally, checked exceptions denote error scenarios outside the program’s immediate control. These usually occur when the program interacts with other systems/network resources e.g. database errors, network connection errors, missing files, etc. Note that all checked exceptions are subclasses of
Checked Exception ExampleThe
In the above example, you will get compile-time error with the message – To make the program able to compile, we must handle this error situation in the try-catch block. Below given code will compile absolutely fine.
3.2. Unchecked ExceptionUnchecked exceptions are not checked by the compiler. These are called runtime exceptions. Unchecked exceptions will come into life and occur in the program, once any buggy code is executed. In Java, the compiler does not force a member method to declare the unchecked exceptions into the method declaration. Generally, such methods almost always do not declare them. Unchecked Exceptions are
subclasses of
The strange thing is that Unchecked Exception ExampleThe code in the given program does not give any compile-time error. But when we run the example, it throws
4. Exception Handling Best Practices
5. ConclusionIn this Java tutorial, we learned about Java exceptions. We learned the difference between checked vs unchecked exceptions in Java and how to handle unchecked exceptions and exception hierarchy in Java with examples. Remember, the biggest difference between checked and unchecked exceptions is that checked exceptions are forced by the compiler and used to indicate exceptional conditions that are out of the program’s control, while unchecked exceptions occur during runtime and are used to indicate programming errors. Happy Learning !! What is unchecked and checked exception?A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.
What is a checked exception?A checked exception is a type of exception that must be either caught or declared in the method in which it is thrown. For example, the java.io.IOException is a checked exception.
What are some unchecked exceptions?Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.
|