0% found this document useful (0 votes)
3 views33 pages

Exception Handling

The document provides an overview of exception handling in Java, explaining the types of exceptions (checked and unchecked) and the mechanisms to handle them, including try-catch blocks and finally blocks. It also discusses nested try-catch blocks, the use of throw and throws keywords, and assertions for testing assumptions in the code. Additionally, it includes code examples demonstrating various exception handling techniques.

Uploaded by

sonalmonal789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views33 pages

Exception Handling

The document provides an overview of exception handling in Java, explaining the types of exceptions (checked and unchecked) and the mechanisms to handle them, including try-catch blocks and finally blocks. It also discusses nested try-catch blocks, the use of throw and throws keywords, and assertions for testing assumptions in the code. Additionally, it includes code examples demonstrating various exception handling techniques.

Uploaded by

sonalmonal789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Exception Handling

Exception- An exception is an unwanted, unexpected, abnormal


situation that occurred at runtime.
The Exception Handling in Java is one of the powerful mechanism to
handle the runtime errors so that the normal flow of the application can
be maintained.
We should have an alternate source through which we can handle the
exception.
Different types of Exception
1. Checked- The exceptions which are checked my complier for
smooth execution of program at runtime. They are commonly
occurred exceptions.
2. Unchecked- The exceptions which are not checked my complier,
they are directly taken care by JVM. They are rarely occurred
exceptions.
Exception Hierarchy
Mechanism to handle the exceptions
The object oriented mechanism has provided following techniques to
handle the exceptions
1. try- it is a block that contains only risky code.
2. catch- it is used to handle the exception.
3. throw
4. throws
5. finally
class Tet
{
public static void main(String[] args)
{
System.out.println ("main method started");
int a=10,b=0,c;
c=a/b;
System.out.println(c);
System.out.println("main method ended");
}
}
class Tet
{
public static void main(String[] args)
{
System.out.println("main method started");
int a=10,b=0,c;
try {
c=a/b;
System.out.println(c);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("main method ended");
}
}
class Tet
{
public static void main(String[] args)
{
System.out.println("main method started");
int a=10,b=0,c;
try {
c=a/b;
System.out.println(c);
}
catch(Exception e)
{
System.out.println(“cannot divide by zero”);
}
System.out.println("main method ended");
}
}
Multiple try catch block
class Ha try
{ {
public static void main(String[] args) int a[]={10,20,30,40};
{
System.out.println(a[5]);
try
}
{
catch(ArrayIndexOutOfBoundsException
int a=20,b=0,c;
b)
c=a/b;
{
System.out.println(c);
}
System.out.println("beyond array limit");
catch(ArithmeticException e) }
{ }
System.out.println("cannot divide by zero"); }
}
class Haa
try
{
{
public static void main(String[] args)
int a[]={10,20,30,40};
{
System.out.println(a[2]);
try
{
}
int a=20,b=2,c; catch(ArrayIndexOutOfBoundsException
b)
c=a/b;
{
System.out.println(c);
System.out.println("beyond array limit");
}
}
catch(ArithmeticException e)
}
{
}
System.out.println("cannot divide by
zero");
Multiple catch block
While working with multiple catch block the Super class exception
catch block must be at the last
class Multi catch(ArrayIndexOutOfBoundsException a)
{ {
public static void main(String[] args) System.out.println("Array Exception");
{ }
try catch(ArithmeticException b)
{ {
int a=10,b=2,c; System.out.println("Arithmetic Exception");
c=a/b; }
System.out.println(c); catch(NumberFormatException c)
{
int arr[]={10,20,30}; System.out.println("Number Exception");
System.out.println(arr[0]); }
catch(Exception d)
String str="ankit"; {
System.out.println(str.toUpperCase());
System.out.println("All Exception");
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=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.

The syntax for enabling assertion


Java -ea class name
Java -enableassertions class name

The syntax for disabling Assertions


Java -da class name
Java -disableassertions class name
import java.util.Scanner;
class Tesstt
{
public static void main(String args[])
{
int value = 15;
assert value >= 20 : " Underweight";
System.out.println("value is " + value);
}
}

You might also like