wellnwill Marathahalli Bangalore

Exception handing Practice

class Test225
{
void m1()
	{
		System.out.println("Hi");
	}
	
public static void main(String[] args)
{
System.out.println("main starts");

System.out.println(50/0);   //abnormal statement
                                       // termination of program
Test225 t=new Test225();
t.m1();
System.out.println("main ends");
}
}

In above program, exception will be generated inside main() method i.e.(50/0) and program execution will be terminated from there only so calling of m1() will never happen.

class Test224
{
	void m1()
	{
		System.out.println("Hi");
	}
public static void main(String[] args)
{
System.out.println("main starts");
try
{
System.out.println(50/0);  //abnormal statement
                                      //termination of program
}
catch(ArithmeticException  e)
{
	System.out.println(e);
}
System.out.println("main ends");
Test224 t=new Test224();
t.m1();
}
}

In above program, exception will be generated inside main() method i.e.(50/0) and program execution will not be terminated from there only as we are using try catch block. Inside try ArithmeticException will be generated and this will be caught by catch block and rest of the code will be executed successfully.

class Test225
{
	void m1()
	{
		System.out.println("Hi");
	}
public static void main(String[] args)
{
System.out.println("main starts");
try
{
System.out.println(50/0);  //abnormal statement// termination of program
}
catch(Throwable  ee)      //generic catch
{
	System.out.println(ee);
}

catch(ArithmeticException  e) //specific catch
{
	System.out.println(e);
}

System.out.println("main ends");
Test224 t=new Test224();
t.m1();
}
}

In above program, specific catch block should come first and then after generic.

class Test
{
	void m1() 
	{
		ArithmeticException  e=new ArithmeticException("/ by zero");
		throw e;
	}
public static void main(String[] args) //caller method
{
System.out.println("main starts");
Test t=new Test();
try
{
 t.m1();
}
catch(Exception  e)
{
	System.out.println(e);
}
System.out.println("main ends");
}
}

In above program, as ArithmeticException object has been created by user so it should be thrown by throw keyword and this object will be propagated to main() method.

class Test227
{
	void m1() 
	{  
	try{
		ArithmeticException  e=new ArithmeticException("/ by zero");
		throw e;
		}
		catch(Exception  e)
        {
     	System.out.println(e);
        }
	}
public static void main(String[] args) //caller mehod
{
System.out.println("main starts");
Test227 t=new Test227();
t.m1();
System.out.println("main ends");
}
}

In above program, We are catching exception object inside m1() method.

class Test228
{
public static void main(String[] args)
{
System.out.println("main starts");
try
{
System.out.println(50/0);          //abnormal statement// termination of program
}
catch(ArithmeticException  e)
{
	System.out.println(e);
}

finally
{
System.out.println("i will be executed always");	
}

}
}

In above program, We are catching the exception object inside main() method and using finally block.

import java.io.IOException;
class Test229
{
	void m1()     // checked exception will not be propagated by default. Please use throws keyword
	{
		IOException  e=new IOException("checked exception");
		throw e;
	}
public static void main(String[] args) //caller method
{
System.out.println("main starts");
Test229 t=new Test229();
try
{
 t.m1();
}
catch(Throwable  e)
{
	System.out.println(e);
}
System.out.println("main ends");
}
}

In above program, m1() method is creating IOException object(i.e.checked exception) so this will not be propagating by dafault to caller method. We have to handle this exception either inside m1() method by using try catch or m1() method exception will be declared to be thrown.

import java.io.IOException;
class Test230
{
	void m1() throws IOException  // checked exception will not be propagated by default. Use throws keyword.
	{
		IOException  e=new IOException("checked exception");
		throw e;
	}
public static void main(String[] args) //caller mehod
{
System.out.println("main starts");
Test230 t=new Test230();
try
{
 t.m1();
}
catch(Exception  e)
{
	System.out.println(e);
}
System.out.println("main ends");
}
}

In above program, m1() method is creating IOException object(i.e.checked exception) so this will not be propagating by dafault to caller method that is why we are declaring m1() method exception by using throws keyword. Now this is caller method(main() method) responsibilty to handle the exception.


user defined checked exception 1

class InvalidAmountException extends Exception //InvalidAmountException is checked class { String msg; InvalidAmountException(String s) { String msg=s; //super(s); //call to super to reuse the string parameter constructor of Exception. } } class Driver { static void checkAmount(int amount) throws InvalidAmountException { if(amount<100)

{ InvalidAmountException ag=new InvalidAmountException("invalid amount"); throw ag; } else System.out.println("valid amount"); } public static void main(String args[]) { try{ checkAmount(10); } catch(Exception e) { System.out.println("Exception: "+e); } System.out.println("Done"); } }

user defined checked exception 2


class InvalidAmountException extends Exception //InvalidAmountException is checked class
{  
 InvalidAmountException(String s)
 {  
  super(s);      //call to super to reuse the string parameter constructor of Exception.  
 }  
} 
class Amount
{  
  static void checkAmount(int amount) throws InvalidAmountException
  {  
     if(amount<100) 
	 {
      InvalidAmountException ag=new InvalidAmountException("invalid amount"); 
      throw ag;	
	 }	  
     else  
      System.out.println("valid amount");  
   } 
   
 public static void main(String args[])
   {  
      try{  
      checkAmount(10);  
      }
      catch(Exception e)
      {
       System.out.println("Exception: "+e);
      }  
System.out.println("Done");  
  }  
}

user defined unchecked exception

class InvalidAmountException extends RuntimeException  
{  
 InvalidAmountException(String s)
 {  
  super(s);  
 }  
} 
class Amount{  
  
   static void checkAmount(int amount) 
   {  
     if(amount<100)

{ InvalidAmountException ae=new InvalidAmountException("amount is invalid"); throw ae; } else System.out.println("valid amount"); } public static void main(String args[]) { try{ checkAmount(99); } catch(Exception e) { System.out.println("Exception message: "+e); } System.out.println("done"); } }
Footer with Map

Top