In this article we will learn how to create user defined exceptions (own exceptions) and how to use them in Java programs.
Although Java provides several pre-defined exception classes, sometimes we might need to create our own exceptions which are also called as user-defined exceptions.
Steps for creating a user-defined exception:
- Create a class with your own class name (this acts the exception name)
- Extend the pre-defined class Exception
- Throw an object of the newly create exception
As an example for user-defined exception, I will create my own exception named NegativeException as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class NegativeException extends Exception { String msg = "Value cannot be negative"; NegativeException() {} NegativeException(String str){ msg = str; } public String toString(){ return “NegativeException: “ + msg; } } |
Note that I am overriding the toString() method of the Exception class to provide meaningful description of my own exception.
Now, I can use my own exception NegativeException in Java programs as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class NegativeExceptionDemo { public static void main(String[] args){ try{ int x = –5; if(x < 0){ throw new NegativeException(); }else{ System.out.println(“x = “ + x); } } catch(NegativeException e){ System.out.println(e); } } } |
Output of the above program is:
NegativeException: Value cannot be negative
From the above program you might have guessed the use of NegativeException. It notifies the user about negative values which are not allowed as input.
This how we can create and use our own exceptions in Java.
A try block can be followed by multiple catch blocks to catch different types of exceptions in the code. Let’s look at a Java program which uses multiple catch blocks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; class ArrayException{ public static void main(String[] args){ try{ Scanner input = new Scanner(System.in); int x = 10; int y = input.nextInt(); int z = x / y; System.out.println(“z = “ + z); int a[] = {1, 2, 3}; System.out.println(“Value is: “ + a[4]); System.out.println(“Another print statement!”); } catch(ArithmeticException e){ System.out.println(e); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } System.out.println(“Outside try-catch block”); } } |
Output of the above program when z is given the value 2 by the user is:
z = 5
java.lang.ArrayIndexOutOfBoundsException: 4
Outside try-catch block
Output of the above program when z is given the value 0 by the user is:
java.lang.ArithmeticException: / by zero
Outside try-catch block
When using multiple catch blocks care should be taken that the order of exception classes must be from sub classes to the super class Exception. For example if the above program is modified as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Scanner; class ArrayException { public static void main(String[] args) { try { Scanner input = new Scanner(System.in); int x = 10; int y = input.nextInt(); int z = x / y; System.out.println(“z = “ + z); int a[] = {1, 2, 3}; System.out.println(“Value is: “ + a[4]); System.out.println(“Another print statement!”); } catch(Exception e) { System.out.println(e); } catch(ArithmeticException e) { System.out.println(e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); } System.out.println(“Outside try-catch block”); } } |
The above program when compiled will generate errors as the second and third catch blocks are non-reachable. To eliminate errors place the Exceptioncatch block as the last catch block.
Take your time to comment on this article.