Exception Handling and creating exception in Java

0
104

In Java programming language, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. Handling in Java is a sophisticated tool for handling runtime faults while keeping the application’s normal flow intact. Exception in Java are raised with throw keyword and handled by catch block.

The java.lang.Throwable class is the root class of Java Exception hierarchy inherited by two subclasses: Exception and Error. The hierarchy of Java Exception classes is given below:

exception in Java

Java Exception provides five different keywords. They are listed below:

KeywordDescription
trySpecifies the block where we should place exception code. Try block must be followed with catch or finally keywords
catchThis keyword is used to handle the exception. It must be preceded by try block which means we can’t use catch block alone. It can be followed by finally block later.
finallyThe “finally” block is used to execute the necessary code of the program. It is executed whether an exception is handled or not.
throwThe “throw” keyword is used to throw an exception.
throwsThe “throws” keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn’t throw an exception. It is always used with method signature.

Java Try-catch block

Java try block

Java try block is used to enclose the code that might throw an exception. It must be used within the method.

If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.

Java try block must be followed by either catch or finally block.

Java Catch block

Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception.The catch block must be used after the try block only. You can use multiple catch block with a single try block.

Syntax of try-catch block

try {

  //  Block of code to try

}

catch(Exception e) {

  //  Block of code to handle errors

}

Sample Program to demonstrate try and catch exception

exception in Java
exception in Java

Java Finally Block

Java finally block is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not.

Cases of finally block used in exception handling

Case 1: When exception does not occur

Source code:

exception in Java

Output:

exception in Java

Case 2: When exception occurred but not handled by catch block

Source code:

exception in Java

Output:

exception in Java

Case 3: When exception occurs and handled by catch block

Source code:

exception in Java

Output:

exception in Java

Java Throw Keyword

The Java throw keyword is used to throw an exception explicitly.We specify the exception object which is to be thrown. The Exception has some message with it that provides the error description. These exceptions may be related to user inputs, server, etc.

Syntax for Java Throw keyword is given below:

throw new exception_class(“error message”);  

Example

Throw an exception if age is below 18 (print “Access denied”). If age is 18 or older, print “Access granted”:

Source code:

exception in Java

Output:

exception in Java

The above code throws an exception because the age given by user does not exceed 18 years old. If we set checkAge(20) the program will not throw any exception as shown below.

exception in Java

Difference between Throw and Throws keyword

ThrowThrows
throw keyword is used to throw an exception explicitly.throws keyword is used to declare one or more exceptions, separated by commas.
Only single exception is thrown by using throw.Multiple exceptions can be thrown by using throws.
throw keyword is used within the method.throws keyword is used with the method signature.
Syntax wise throw keyword is followed by the instance variable.Syntax wise throws keyword is followed by exception class names.
Checked exception cannot be propagated using throw only.Unchecked exception can be propagated using throw.For the propagation checked exception must use throws keyword followed by specific exception class name.

Click here to know about concurrency and threading concept in Java

Facebook Comments