Exception Handling Examples
Example of Throw and Throws
Example of Multiple Catch Block
Example of Null Pointer Exception
Example of Creating Own Exception
Example of Number Format Exception
Example of ArrayIndexOutOfBoundsException
Throw and Throws
class Demo{
private int num;
public void display(int n) throws Exception{
if( n == 0 ){
throw new Exception("canot assign zero");
}
else if(n < 0){
throw new Exception("canot assign -ve value");
}
else{
System.out.println("\n\n\tValid value");
num = n;
}
}
}
public class ThrowsDemo{
public static void main(String args[]){
Demo ob = new Demo();
try{
ob.display(-8);
//ob.display(10);
}
catch(Exception e){
System.out.println("\n Error: "+ e);
}
System.out.println("\n\n");
}
}
Output
Error: java.lang.Exception: canot assign -ve value
Example of Multiple Catch Block
Multiple catch Block
class Test1{
public static void main(String args[]){
int a,b;
try
{
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
System.out.println(a/b);
}
catch(NumberFormatException ee)
{
System.out.println("Number Format Exception Raised "+ee);
}
catch(ArithmeticException ee)
{
System.out.println("ArithmeticException Raised because
you divide a number with zero "+ee);
}
catch(Exception ee) //this block gona be last
{
System.out.println("Miscellaneous Exception Raised"+ee);
}
}
}
Output
Miscellaneous Exception Raisedjava.lang.ArrayIndexOutOfBoundsException: 0
Example of Null Pointer Exception
NullPointer Exception
public class Check{
public static void main(String[] args) {
String a = null;
String b = "Hello";
if(b!=null){
System.out.println("B is "+b.length());
}
if(a!=null){
System.out.println("A is "+a.length());
}
System.out.println("Now This line never execute...")
}
}
Output
B is 5 Now This line never execute...
Example of Creating Own Exception
Creating Own Exception
//Step-1 Creating Custom Exception class
import java.io.*;
class MyException extends Exception{
int age;
MyException(){
}
MyException(int age){
this.age=age;
}
public String toString(){
return "Age Cannot be less than 18 and your
entered age is "+age+" So u can't vote";
}
}
//Step-2 Using CustomException
public class ExceptionDemo{
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Age");
int age=Integer.parseInt(br.readLine());
try{
if(age<18)
throw new MyException(age);
else
System.out.println("Yes you can vote ");
}
catch(MyException ee){
System.out.println(ee);
}
}
}
Output
Enter the Age 40 Yes you can vote
Example of Number Format Exception
NumberFormatException
class Test{
public static void main(String args[]) {
int a,b;
try{
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
System.out.println(a+b);
}
catch(NumberFormatException ee){
System.out.println("Number Format Exception
Raised because you not enter number between 0-9"+ee);
// a stack trace from where the exception
//occurred will be printed.
ee.printStackTrace();
}
}
}
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Test.main(Test.java:9)
Example of ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException
class Test{
public static void main(String args[]){
try {
int x[]=new int[10];
x[11]=100;
}
catch(ArrayIndexOutOfBoundsException ee) {
System.out.println(ee);
}
}
}
Output
java.lang.ArrayIndexOutOfBoundsException: 11



