Exception Handling
Exception Handling
The Exception Handling in Java is one of the powerful mechanism to handle the runtime
errors so that normal flow of the application can be maintained.
In Java, an exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime
The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited
by two subclasses: Exception and Error. A hierarchy of Java Exception classes are given
below:
Keyword Description
try The "try" keyword is used to specify a block where
It must be preceded by try block which means we can't use catch block alone.
finally The "finally" block is used to execute the important code of the program.
There are mainly two types of exceptions: checked and unchecked. Here, an error is
considered as the unchecked exception. According to Oracle, there are three types of
exceptions:
Checked Exception
Unchecked Exception
Error
3) Error
class Ae
int a=10,b=0;
try
System.out.println("division="+(a/b));
}
catch(ArithmeticException e)
finally{
Output:
it is always ececuted
class Ex2
int a=0,b=10;
String s="java";
try{
System.out.println(b/a);
int i=Integer.parseInt(s);
System.out.println(i);
s=null;
System.out.println(s.length());
catch(ArithmeticException e)
catch(NullPointerException e)
{
catch(NumberFormatException e)
Output:
/ by zero exception
1) Checked: are the exceptions that are checked at compile time. If some code within a
method throws a checked exception, then the method must either handle the exception or
it must specify the exception using throws keyword.
For example, consider the following Java program that opens file at location “C:\test\a.txt”
and prints the first three lines of it. The program doesn’t compile, because the function
main() uses FileReader() and FileReader() throws a checked
exception FileNotFoundException. It also uses readLine() and close() methods, and these
methods also throw checked exception IOException
2) Unchecked are the exceptions that are not checked at compiled time. In C++, all
exceptions are unchecked, so it is not forced by the compiler to either handle or specify the
exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions,
everything else under throwable is checked.
A try block can be followed by one or more catch blocks. Each catch block must contain a
different exception handler. So, if you have to perform different tasks at the occurrence of
different exceptions, use java multi-catch block.
At a time only one exception occurs and at a time only one catch block is executed.
All catch blocks must be ordered from most specific to most general, i.e. catch for
ArithmeticException must come before catch for Exception
Java finally block is a block that is used to execute important code such as closing
connection, stream etc.
he throw keyword
You can throw a user-defined exception or, a predefined exception explicitly using
the throw keyword.
There are two types of exceptions user-defined and predefined each exception is
represented by a class and which inherits the Throwable class.
To throw an exception explicitly you need to instantiate the class of it and throw its
object using the throw keyword.
Example
1. public class TestThrow {
2. //defining a method
3. public static void checkNum(int num) {
4. if (num < 1) {
5. throw new ArithmeticException("\nNumber is negative, cannot calculate square");
6. }
7. else {
8. System.out.println("Square of " + num + " is " + (num*num));
9. }
10. }
11. //main method
12. public static void main(String[] args) {
13. TestThrow obj = new TestThrow();
14. obj.checkNum(-3);
15. System.out.println("Rest of the code..");
16. }
17. }
Output:
Number is negative, cannot calculate square
public class TestThrows {
2. //defining a method
3. public static int divideNum(int m, int n) throws ArithmeticException {
4. int div = m / n;
5. return div;
6. }
7. //main method
8. public static void main(String[] args) {
9. TestThrows obj = new TestThrows();
10. try {
11. System.out.println(obj.divideNum(45, 0));
12. }
13. catch (ArithmeticException e){
14. System.out.println("\nNumber cannot be divided by 0");
15. }
16.
17. System.out.println("Rest of the code..");
18. }
19. }
Output: