[go: up one dir, main page]

0% found this document useful (0 votes)
2 views20 pages

Exception_Handling

Exception handling in Java is a mechanism to manage runtime errors and maintain the normal flow of applications. It includes checked exceptions, which are verified at compile-time, and unchecked exceptions, which are verified at runtime. Key components of exception handling include try, catch, finally, throw, and throws keywords, which help in managing exceptions effectively.

Uploaded by

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

Exception_Handling

Exception handling in Java is a mechanism to manage runtime errors and maintain the normal flow of applications. It includes checked exceptions, which are verified at compile-time, and unchecked exceptions, which are verified at runtime. Key components of exception handling include try, catch, finally, throw, and throws keywords, which help in managing exceptions effectively.

Uploaded by

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

Exception Handling

The Exception Handling in Java is one of


the powerful mechanism to handle the
runtime errors so that normal flow of the
application can be maintained.

Exception Exception is an abnormal condition.


Handling:

In java, an exception is an event that


disrupts the normal flow of the program.
What is Exception Handling

► Exception Handling is a mechanism to handle runtime errors such as


ClassNotFoundException, IOException, SQLException,
RemoteException, etc.

► The core advantage of exception handling is to maintain the normal


flow of the application.

► Suppose there are 10 statements in your program and there occurs


an exception at statement 5, the rest of the code will not be
executed i.e. statement 6 to 10 will not be executed. If we perform
exception handling, the rest of the statement will be executed. That
is why we use exception handling in Java.
Types of Java
Exceptions:

► There are mainly two types of


exceptions:
► checked and unchecked.
► Here, an error is considered
as the unchecked exception.
According to Oracle, there
are three types of
exceptions:
Checked Exception

► The classes which directly inherit Throwable class except


RuntimeException and Error are known as checked exceptions e.g.
IOException, SQLException etc. Checked exceptions are checked
at compile-time.
Unchecked Exception

► The classes which inherit RuntimeException are known as


unchecked exceptions e.g. ArithmeticException,
NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they
are checked at runtime.

► Error: Error is irrecoverable e.g. OutOfMemoryError,


VirtualMachineError, AssertionError etc.
Exception Keyword Description

► try The "try" keyword is used to specify a block where we should


place exception code. The try block must be followed by either
catch or finally. It means, we can't use try block alone.
► catch The "catch" block is used to handle the exception. It must
be preceded by try block which means we can't use catch block
alone. It can be followed by finally block later.
► finally The "finally" block is used to execute the important code of
the program. It is executed whether an exception is handled or not.
► throw The "throw" keyword is used to throw an exception.
► throws The "throws" keyword is used to declare exceptions. It
doesn't throw an exception. It specifies that there may occur an
exception in the method. It is always used with method signature.
Example:
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}

int a[]=new int[5];


a[10]=50; //ArrayIndexOutOfBoundsException

String s=null;
System.out.println(s.length());//NullPointerException
class Ex
{
public static void main(String args[])
{
int a[]=new int[5];
int no=0;
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++)
{
System.out.println("Enter the number:");
a[i]=sc.nextInt();
}
for(int i=0;i<5;i++)
{
System.out.println("The number is:"+a[i]);

}
System.out.println("Enter the number to find
index:");
no=sc.nextInt();
System.out.println("The number is:"+a[no]);
}
}
Java Multi-catch block
try{
int a[]=new int[5];
► A try block can be followed a[5]=30/0;
}
by one or more catch
catch(ArithmeticException e)
blocks. Each catch block {
must contain a different System.out.println("Arithmetic Exception
exception handler. So, if you occurs");
have to perform different }
tasks at the occurrence of catch(ArrayIndexOutOfBoundsException e)
different exceptions, use {
java multi-catch block.
System.out.println("ArrayIndexOutOfBounds
Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception
occurs");
}
► Java finally block is a block that is used to
Java finally execute important code such as closing
connection, stream etc.
block ► Java finally block is always executed whether
exception is handled or not.
► Java finally block follows try or catch block.
Example:

try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
Code import java.util.*;
class dddd
without {
public static void main(String[] args) {
int n1;
Exception int ans;

Handling is Scanner sc=new Scanner(System.in);


System.out.println("Please Enter Number:");
needed? n1=sc.nextInt();
ans=n1/0;
System.out.println("Number can not be divided by
zero.");
System.out.println("This is a Program of demonstartion of
exception handling.");
}
}
import java.util.*;
Why class dddd
{
Exception public static void main(String[] args) {
int n1;

Handling is int ans;

Scanner sc=new Scanner(System.in);


needed? System.out.println("Please Enter Number:");
n1=sc.nextInt();
try
{
ans=n1/0;

}
catch(ArithmeticException ex)
{
System.out.println("Number can not be divided by zero.");
}
System.out.println("This is a Program of demonstartion of
exception handling.");
}
}
Java Nested
import java.util.*;
try block
class dddd try
{ {
public static void main(String[] args) { int a[]=new int[5];
int n1; System.out.println("Array is:"+a[5]);
int ans; }
catch(ArrayIndexOutOfBoundsException ex)
Scanner sc=new Scanner(System.in); {
System.out.println("Please Enter Number:"); System.out.println("Array Exception arrived.");
n1=sc.nextInt(); }
try
{ }
try catch(Exception e)
{ {
ans=n1/0; System.out.println("Exception occured");
} }
catch(ArithmeticException ex) System.out.println("This is a Program of
{ demonstartion of exception handling.");
System.out.println("Number can not be }
divided by zero."); }
}
Java throw keyword

► The Java throw keyword is used to explicitly throw an exception.


► We can throw either checked or uncheked exception in java by
throw keyword. The throw keyword is mainly used to throw custom
exception

if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
import java.io.*;
Java throws class M{
keyword void method()throws IOException{
throw new IOException("device error");
}
► The Java throws keyword is }
used to declare an public class Testthrows2{
exception. It gives an public static void main(String args[]){
information to the try{
programmer that there may M m=new M();
occur an exception so it is
m.method();
better for the programmer to
provide the exception }catch(Exception e){System.out.println("exception
handled");}
handling code so that
normal flow can be
maintained. System.out.println("normal flow...");
}
}
In case you declare the exception, if exception does not occur, the code will be
executed fine.
B)In case you declare the exception if exception occures, an exception will be
thrown at runtime because throws does not handle the exception.

import java.io.*;
class M{
void method()throws IOException{
System.out.println("device operation performed");
}
}
class Testthrows3{
public static void main(String args[])throws
IOException{//declare exception
M m=new M();
m.method();

System.out.println("normal flow...");
}
}
Exception Handling:
class TestCustomException1{

static void validate(int age)throws


class InvalidAgeException extends Exception{ InvalidAgeException{
InvalidAgeException(String s){ if(age<18)
super(s); throw new InvalidAgeException("not valid");
} else
} System.out.println("welcome to vote");
}

public static void main(String args[]){


try{
validate(13);
}catch(Exception
m){System.out.println("Exception occured: "+m);}

System.out.println("rest of the code...");


}
}

You might also like