Last Updated: 10 July, 2023
An exception is an unexpected event that occurs during program execution and affects the current flow of the program execution, which can cause the program to terminate abnormally.
An exception can occur for a couple of reasons. Some of them are:
In Java, the exception handling mechanism helps in minimizing and recovering from exceptions. Java's exception handling technique provides mechanisms to handle runtime exceptions, makes Java programs bug-free, and also helps in maintaining the flow of the program.
As given in the image above, all the exceptions and errors are available in the java.lang package, and the Throwable class is the root class in the hierarchy that is split into two branches: Exception and Error.
An error represents a condition that cannot be recovered, which means a programmer cannot handle the error through the coding, such as the Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite recursion, etc. A programmer usually cannot control errors, so we should not try to fix them.
Exceptions can be caught and handled by the program. When an exception occurs in a Java program, the JVM creates an exception object. This object contains all the information about the exception, such as the name and description of the exception and the state of the program when the exception occurred.
In Java, exceptions have been categorized into two types, and they are as follows:
1. Checked Exception: An exception that is checked by the compiler at the time of compilation is called a checked exception.
2. Unchecked Exception: An exception that cannot be caught by the compiler but occurs at the run (execution) time of the program is called an unchecked exception.
Java has provided five keywords to handle exceptions in Java programs: try, catch, finally, throw, and throws.
Keywords | Description |
try | Contains one or more lines of code that may cause an exception. The try block must be followed by either catch or finally. |
catch | Used to handle the exception that is generated by try block. It must be preceded by try block. For the one try block, catch block may be one or more than one. |
finally | It is an optional block. This block must be executed irrespective of an exception's occurrence. |
throw | Used to throw an exception. |
throws | It specifies the exceptions that a method can throw. It is always used with the method signature. |
An Example of Exception Handling in Java
Output
java.lang.ArithmeticException: / by zero
Output: 0
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between throw and throws keyword in Java?
Ans. The throw and throws keywords are used in Java exception handling, but they have different functionality and uses, as shown in the table below.
throw | throws |
The throw keyword is used to throw an exception object explicitly. | The throws keyword is used to declare an exception as well as bypass the caller. |
throw allows us to declare only one exception at a time. | throws allow us to declare multiple exceptions at a time. |
throw is used inside the method body. | throws is used with the method signature. |
throw is followed by an instance. | throws is followed by a class name. |
Only unchecked exceptions can be propagated usign the throw keyword. | throws keyword can be used to propagate checked exceptions only. |
throw new StudentRecordNotFound(“Student record is not available.”); | throws IOException, ArrayIndexBoundException; |
Is it mandatory for a catch block to be followed after a try block?
Ans. In Java, it is not mandatory to have a catch block immediately following a try block. However, if we use a try block, we must either provide a catch block or a finally block (or both) to handle or manage any potential exceptions that might occur within the try block.
The catch block(s) are used to specify the exception type(s) that we want to handle and provide the corresponding code to handle those exceptions. The finally block, if present, is used to specify code that should be executed regardless of whether an exception occurred or not.
If we write a return statement at the end of the try block, will the finally block be executed or not?
Ans. Yes, the finally block will still be executed even if a return statement is present within the try block. The purpose of the finally block is to contain code that should be executed regardless of whether an exception occurred or not. It ensures that certain actions, such as closing resources or releasing locks, are performed even if an exception is thrown or a return statement is encountered.