218. Exception is an Object

Pre-requisites -

A Java exception is an object that describes an exceptional condition (i.e. error) that has occurred in the piece of code. When an exceptional condition arises, an object representing that condition is created and thrown. 

What happens when we don't handle that exception ?

The program execution will stop at the line of the code where the exceptional condition (i.e. error) got occurred. 

How does the program execution stops when exceptional condition got occurred ?

As our program, don't have any code to handle the occurred exceptional condition (i.e. error), JVM stops our program execution and prints what went wrong using the thrown exception (i.e object). 

How to handle the exception ?

We can handle the exception using the try{ } catch{ } blocks. 

What happens when we use the try{ } catch{ } blocks to handle the exceptions ?

When an exception occurs in try { } block,  instead of passing the exception (i.e. object) to JVM, the try{} block will pass it to its associated catch{ } block where we can write our code to handle the occurred exceptional condition. catch{ } block catches the exception (i.e. object) that describes the occurred exceptional condition in the program code of the try{ } block and executes the program code that is written in its block to handle the occurred exception condition (i.e. error).

Lets understand it using the below code -

    int a = 1/0;  //Gives Arithmetic exception

catch(Exception e)   //Here e is an object of Exception Class type which receives the exception (i.e. object) that is occurred in and passed by try block.
     System.out.println(" 1/0  is not a valid statement ");  //This statement will be printed and the program execution will continue

Lets understand the above code step by step -

1. int a = 1/0;  statement gives divide by zero error and hence throws an exception (i.e. object). But as we have provided the above statement in the try{ } block, instead of passing the exception (i.e. object) to the JVM and terminating the program, try{ } block will pass the exception (i.e. object) to its corresponding catch { } block where we have written the code to handle the exception (i.e. object) to overcome its exceptional condition (i.e. occurred error). 

2. catch(Exception e) -> catch block catches the exception(i.e. object) passed by the try{ } block and assigns it to the Exception Class type object 'e'. You can create Exception Class type object with any name. Lets say, If I want to give the object name as  'object1' instead of 'e' , I can as -> catch(Exception object1). And also Exception is a Parent Class of all its Sub-Classes which handles different types of  Exceptions. In this example, we are using Exception Class i.e parent class, as we know that arithmetic exception is thrown by the statement in the try{ } block, we can use the Class which can receive the exception directly instead of up-casting the received object to the Exception Parent Class and using it. So in order to deal with the exception (i.e. object) directly we can modify the statement as -> catch(ArithmeticException object1). I will explain the Exception Hierarchy in our upcoming posts to explain the parent classes and child classes which are used in catch{ } blocks to handle the exceptions.

3. No programmer will intentionally write the statement int a = 1/0; in his program code. I have intentionally used this statement to make the program throw an exception in this example. But in real time, when a User runs the program created by a programmer, where in the code, the programmer has specified the statement as int c = a/b; and while the User is executing the program created by the programmer, the User may be asked to input a value for a & b variables, and unknowing the User inputs the a variable as 1 and b variable as 0. The exception will occur in the statement int a=b/c and the program created by the programmer may not be run properly at the User end. In order to avoid this kind of situations, the programmer will handle the exceptions in his code by printing the message '0 is not valid' at User end. When User runs the properly handled code at his end, and in case he enter '0' for b variable, the executing program asks the User not to enter the value '0' for the variable b. 

Please comment below to feedback or ask questions.

Exception Hierarchy will be explained in the next post.

No comments: