What does a checked exception mean?
In this Java exceptions tutorial, learn what is an exception in Java, 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?
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 that what all 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 Exception3.1. Exception HierarchyIn Java, exceptions are broadly categorized into two sections:
3.2. Checked ExceptionsJava 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.
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 so that we can inform the user about the failure and ask him to retry or come later. Generally, checked exceptions denote error scenarios that are outside the immediate control of the program. These occur usually interacting 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 Example
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.3. Unchecked Exception
Unchecked exceptions will come into life and occur in the program, once any buggy code is executed. In Java, a member method is not forced by the compiler to declare the unchecked exceptions into the method declaration. Generally, such methods almost always do not declare them. Unchecked Exceptions are subclasses of
Unchecked Exception ExampleThe code in the given program does not give any compile-time error. But when we run the example, it throws
3. Exception Handling Best Practices
In reality, most applications will have to recover from pretty much all exceptions
including The only time it is normally legal to shut down an application is during startup. For example, if a configuration file is missing and the application cannot do anything sensible without it, then it is legal to shut down the application. 4. ConclusionIn this Java tutorial, we learned about Java exceptions. We learned the difference between checked vs unchecked exceptions in Java, along with how to handle unchecked exceptions, 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 control of the program, while unchecked exceptions are occurred during runtime and are used to indicate programming errors. Happy Learning !! What is an example of a checked exception?ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions.
Are checked exceptions good?Checked exceptions leads to annoying boilerplate code ( try {} catch () {} ). Every time you call a method that throws a checked exception, you have to write the try-catch-statement. The compiler forces us to catch the exception. Often this ends up in a mixing of main logic and error handling.
Why do we need checked exceptions?“If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.” In this way, we can recover the system by accepting another user input file name.
What are checked and unchecked exceptions examples?Compare checked vs. unchecked exceptions. |