Unit 8 Exception Handling
Unit 8 Exception Handling
Yes
Yes No
Note:
1. In java all the exception are class of throwable type.
2. The exception will occurred because of statement.
3. The statement is responsible to generates an exception.
• Type of Exceptions
1. Checked Exception:
In this case our compiler already knows which particular statement is responsible to generate
an exception hence the compiler will force the programmer to either handled the exception or declared
the exception.
If not our compiler will never allow us to proceed further still if you try to proceed further we
will get Compile time error say in unreported exception.
Ex.
InterruptedException
FileNotFoundException
2. Unchecked Exception:
In this case our compiler don’t known which particular statement is responsible to
generate an exception.
Hence our compiler will not force the programmer to either handled and declared the
exception. Therefor we don’t get any compile time error even if we neither handled nor
declared the exception.
Ex. All the runtime exceptions and it’s subclasses along with error and it is subclasses are
unchecked exception.
• Throwable class:
3. In throwable class and it subclasses the toString( ) method is overridden in such a way
that it’s returns the fully qualified name of the exception along with reason.
1. In throwable hierarchy the error class and it’s subclasses as well as runtime exception and
it’s subclass are considered as unchecked exception.
2. Exception class and it’s subclasses except runtime exception are considered as checked
exception.
3. The exception and throwable class are considered as partially checked classes.
• Exception Handling:
1. The statements which are responsible for causing an exception should be written inside
try Block.
1. The catch block is used to stored the reference of throwable type object which is thrown by the try block.
2. When the catch block is capable of storing the reference of throwable object then we can say the exception
is handled. and that particular catch block will get executed.
3. if the catch block doesn’t store the reference of throwable type object then we can say exception is not
handled.
4. When the exception is handled normal flow will continues in case of the exception is not handled then the
program will stop abruptly.
Ans: The throwable type reference variable which is declared inside catch block store the reference of throwable
type object then we can say the exception is handled.
• Important Exception
1. Arithmetic Exception
This is the most common cause of an Arithmetic Exception. For example, if you try to divide a
number by zero (e.g., int result = 5 / 0;), it will result in an Arithmetic Exception because dividing by
zero is undefined in mathematics. When you attempt to calculate the remainder of a division operation
by zero (e.g., int remainder = 10 % 0;), it will result in an Arithmetic Exception.
ii. Arithmetic exception is a throwable type because throwable class is super most class.
iii. In this case the exception is occurred and not handled hence the program got stop abruptly.
In this example, when you attempt to divide numerator by denominator, which is zero, an
ArithmeticException is thrown, and the catch block handles the exception by printing a message.
Solution:
class Exception
{
public static void main (String [] args)
{
try
{
System.out.println("From try Block");
int a= 10/0;
System.out.println(a);
}
catch (ArithmeticException ae)
{
System.out.println("From Catch Block Exception got Handled");
}
}
}
2. Array Index Out of Bounds Exception
When we try to access the element which is not in the given range then we got
ArrayIndexOutOfBoundsException.
When we try to perform some action on null value, we get null pointer exception. To
avoid NullPointerException errors, you should always check whether an object reference is
null before attempting to access its methods, variable, or elements. You can use conditional
statements, such as if or the null-safe operator ( in Java 8 and later), to handle these cases
gracefully and avoid unexpected runtime exceptions.
Solution:
class Exception
{
public static void main (String [] args)
{
try
{
System.out.println("From try Block");
String s=null;
System.out.println(s.toUpperCase()):
System.out.println(s.length());
}
catch (NullPointerException np)
{
System.out.println("Exception got handled");
}
}
}
5. Number Format Exception: