try
{
//code that cause exception;
}
catch(Exception_type e)
{
//exception handling code
}
class TryCatchDemo
{
public static void main(String args[])
{
int a = 30, b = 3, c = 3;
int result1, result2;
try
{
result1 = a / (b - c);
}
catch(ArithmeticException ae)
{
System.out.println("Divided by zero: "+ae);
}
result2 = a / (b + c);
System.out.println("Result2 = "+result2);
}
}
class ExceptionDemo
{
public static void main(String args[])
{
try
{
int arr[] = new int[5];
arr[2] = 5;
System.out.println("Access element two: " + arr[2]);
arr[7] = 10;
System.out.println("Access element seven: "+ arr[7]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown:" + e);
}
System.out.println("End of the block");
}
}
try
{
// code which generate exception
}
catch(Exception_type1 e1)
{
//exception handling code
}
catch(Exception_type2 e2)
{
//exception handling code
}
public class MultipleCatch
{
public static void main(String args[])
{
try
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a / b;
System.out.println("Result = "+c);
}
catch(ArithmeticException ae)
{
System.out.println("Enter second value except zero.");
}
catch (ArrayIndexOutOfBoundsException ai)
{
System.out.println("Enter at least two numbers.");
}
catch(NumberFormatException npe)
{
System.out.println("Enter only numeric value.");
}
}
}
try
{
// code
}
catch(Exception_type1)
{
// catch block1
}
Catch(Exception_type2)
{
//catch block 2
}
finally
{
//finally block
//always execute
}
class FinallyTest
{
public static void main(String args[])
{
int arr[] = new int[5];
try
{
arr[7] = 10;
System.out.println("Seventh element value: " + arr[7]);
}
catch(ArrayIndexOutOfBoundsException ai)
{
System.out.println("Exception thrown : " + ai);
}
finally
{
arr[0] = 5;
System.out.println("First element value: " +arr[0]);
System.out.println("The finally statement is executed");
}
}
}
public class ThrowTest
{
static void test()
{
try
{
throw new ArithmeticException("Not valid ");
}
catch(ArithmeticException ae)
{
System.out.println("Inside test() ");
throw ae;
}
}
public static void main(String args[])
{
try
{
test();
}
catch( ArithmeticException ae)
{
System.out.println("Inside main(): "+ae);
}
}
}
public class ThrowsDemo
{
static void throwMethod1() throws NullPointerException
{
System.out.println ("Inside throwMethod1");
throw new NullPointerException ("Throws_Demo1");
}
static void throwMethod2() throws ArithmeticException
{
System.out.println("Inside throwsMethod2");
throw new ArithmeticException("Throws_Demo2");
}
public static void main(String args[])
{
try
{
throwMethod1();
}
catch (NullPointerException exp)
{
System.out.println ("Exception is: " +exp);
}
try
{
throwMethod2();
}
catch(ArithmeticException ae)
{
System.out.println("Exception is: "+ae);
}
}
}
Throw | Throws |
---|---|
It is used to throw own exception. | It is used when program does not handle exception via try block. |
It handles explicitly thrown exceptions. | It handles all exceptions generated by program. |
Cannot throw multiple exceptions. | Declare multiple exception e.g. public void method() throws, IOException, SQLException. |
It is used within the method. | It is used within method signature. |