Exception Handling
Exception Handling
try {
{ System.out.println("Arithmetic Exceptin");
int a=10,b=0,c; }
c=a/b; catch(NumberFormatException c)
System.out.println(c); {
System.out.println("Number Exception");
int arr[]={10,20,30}; }
System.out.println(arr[0]); catch(Exception d)
{
String str="ankit"; System.out.println("All Exception");
System.out.println(str.toUpperCase());
}}}
}
catch(ArrayIndexOutOfBoundsException a)
{
class Multi
System.out.println("Array Exception");
{
}
public static void main(String[] args)
catch(ArithmeticException b)
{
try {
{ System.out.println("Arithmetic Exceptin");
int a=10,b=2,c; }
c=a/b; catch(NumberFormatException c)
System.out.println(c); {
System.out.println("Number Exception");
int arr[]={10,20,30}; }
System.out.println(arr[6]); catch(Exception d)
{
String str="ankit"; System.out.println("All Exception");
System.out.println(str.toUpperCase());
}}}
}
catch(ArrayIndexOutOfBoundsException a)
class Multi
{
{
System.out.println("Array Exception");
public static void main(String[] args)
}
{
catch(ArithmeticException b)
try {
{ System.out.println("Arithmetic Exceptin");
int a=10,b=2,c; }
c=a/b; catch(NumberFormatException c)
System.out.println(c); {
System.out.println("Number Exception");
int arr[]={10,20,30}; }
System.out.println(arr[2]); catch(Exception d)
{
String str=null; System.out.println("All Exception");
System.out.println(str.toUpperCase()); }}}
}
Finally Block
Finally block is the realtime block and the main purpose of the finally
block is to handle the resources.
class Hand
{
public static void main(String[] args)
{
try
{
System.out.println("Learn");
int a=20,b=2,c;
c=a/b;
System.out.println(c);
System.out.println("like");
}
catch(Exception e)
{
System.out.println("cannot divide by zero");
}
finally
{
System.out.println("share");
}
System.out.println("main method ended");
}
}
class Han
{
public static void main(String[] args)
{
try
{
System.out.println("Learn");
int a=20,b=0,c;
c=a/b;
System.out.println(c);
System.out.println("like");
}
catch(Exception e)
{
System.out.println("cannot divide by zero");
}
finally
{
System.out.println("share");
}
System.out.println("main method ended");
}
}
catch(Exception e)
{
int x=20,y=0,z;
z=x/y;
class Hann System.out.println(z);
{
System.out.println("cannot divide by zero");
public static void main(String[] args)
}
{
finally
try
{
{
System.out.println("share");
System.out.println("Learn");
}
int a=20,b=0,c;
System.out.println("main method ended");
c=a/b;
}
System.out.println(c);
}
System.out.println("like");
}
Nested try Block
A try block which contains another try block inside is called
nested try block.
class Ness
{
System.out.println(10/0);
public static void main (String[] args)
{
try }
{ catch(ArithmeticException e)
try
{
{
int a[]={10,20,30}; System.out.println(e);
System.out.println(a[2]); }
}
System.out.println("learn");
catch(ArrayIndexOutOfBoundsException a)
{
}
System.out.println(a); }
}
Nested catch Block
A catch block which contains another catch block inside is called nested
catch block.
class Nesss
{
public static void main (String[] args)
{
try
{
System.out.println(10/0);
}
catch(Exception e)
{
String a=null;
System.out.println(a.toLowerCase());
}
System.out.println("main method ended");
}
}
try
class Nessss {
String a=null;
{
System.out.println(a.toLowerCase());
public static void main (String[] args) }
{ catch(NullPointerException n)
try {
System.out.println("null value cannot be
{ converted");
System.out.println(10/0); }
} }
System.out.println("main method ended");
catch(Exception e)
}
{ }
throw
throw keyword is used to throw the user defined or customised
exception object to the JVM explicitly
class Th
{
public static void main(String[] args)
{
int balance=10000;
balance=balance-10000;
if(balance==0)
throw new ArithmeticException(“no balance”);
}
}
throws Keyword
throws keyword is used when we do not want to handle the exception
and try to send the exception to JVM.
class Thro
{
public static void main(String[] args)
{
for (int i=1;i<=10;i++)
{
System.out.println(i);
Thread.sleep(1000);
}
}
}
class Thro
{
public static void main(String[] args) throws InterruptedException
{
for (int i=1;i<=10;i++)
{
System.out.println(i);
Thread.sleep(1000);
}
}
}
Assertion
Assertion is a statement in java. It can be used to test your assumptions
about the program.
While executing assertion, it is believed to be true. If it fails, JVM will
throw an error named AssertionError. It is mainly used for testing
purpose.
SYNTAX
assert expression;
assert expression1: expression2;
By default, assertions are disabled.