0% found this document useful (0 votes)
6 views6 pages

Exception

The document explains exception handling in Java, categorizing exceptions into checked, unchecked, and errors. It details the keywords used for exception handling, including try, catch, throw, throws, and finally, along with examples for each. Additionally, it clarifies the differences between the throw and throws keywords and provides examples demonstrating their usage.

Uploaded by

solankesnehal96k
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)
6 views6 pages

Exception

The document explains exception handling in Java, categorizing exceptions into checked, unchecked, and errors. It details the keywords used for exception handling, including try, catch, throw, throws, and finally, along with examples for each. Additionally, it clarifies the differences between the throw and throws keywords and provides examples demonstrating their usage.

Uploaded by

solankesnehal96k
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/ 6

EXCEPTION IN JAVA

Exception Handling
Exception Handling is the mechanism to handle runtime malfunctions.
Exception are categorized into 3 category.

 Checked Exception

The exception that can be predicted by the programmer at the compile time.
Example : File that need to be opened is not found. These type of exceptions must
be checked at compile time.

 Unchecked Exception

Unchecked exceptions are the class that extends RuntimeException. Unchecked


exception are ignored at compile time. Example : ArithmeticException,
NullPointerException, Array Index out of Bound exception. Unchecked exceptions
are checked at runtime.

 Error

Errors are typically ignored in code because you can rarely do anything about an
error. Example :if stack overflow occurs, an error will arise. This type of error cannot
be handled in the code.

Exception Handling Mechanism


In java, exception handling is done using five keywords,

try: try keyword is used for identifying code block which may cause exception at
runtime.

catch: catch keyword handle the exception if try block raises exception. The code under
try block if raises runtime error, try block sends handler to catch block to handle error.

throw: throw keyword is used for creating user defined exception messages.

Throws: gets a prior knowledge about which exceptions are to be handled.it doesn’t throw
an exception.it is always used with method signature.

finally: finally block executed whether exception is raised or not. It is used for cleaning resource
and executing set of code.

pg. 1 Amol Shinde


EXCEPTION IN JAVA

Example using Try and catch


class Excp
{
public static void main(String args[])
{
int a,b,c;
try
{
a=0;
b=10;
c=b/a;
System.out.println("This line will not be executed");
}
catch(ArithmeticException e)
{
System.out.println("Divided by zero");
}
System.out.println("After exception is handled");
}
}

Divided by zero

After exception is handled

Example for Multiple Catch blocks


class Excep
{
public static void main(String[] args)
{
try
{
int arr[]={1,2};
arr[2]=3/0;
}
catch(ArithmeticException ae)
{
System.out.println("divide by zero");

pg. 2 Amol Shinde


EXCEPTION IN JAVA

}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index out of bound exception");
}
}
}

divide by zero

Note: Although both ArrayIndexOutOfBoundsException and ArithmeticException occured,


but since first catch is of Arithmetic Exception, It will be caught there and program control
will be continued after the catch block.
Note: At a time, only one exception is processed and only one respective catch block is
executed.

throw Keyword
throw keyword is used to throw an exception explicitly.
import java.util.*;
class UserEx
{
public static void main(String[]args)
{
try
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");


int a=sc.nextInt();
if(a>0 && a<25)
{
throw (new SmallException());
}
else if(a>26 && a<50)
{
throw (new AvgException());
}
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}

pg. 3 Amol Shinde


EXCEPTION IN JAVA

class SmallException extends Exception


{
public SmallException()
{
}
}
class AvgException extends Exception
{
public AvgException()
{
}
}

Enter the number


20
SmallException

throws Keyword
Any method that is capable of causing exceptions must list all the exceptions possible
during its execution, so that anyone calling that method gets a prior knowledge about which
exceptions are to be handled. A method can do so by using the throws keyword.

import java.util.*;
class Test
{
static void check() throws ArithmeticException
{
System.out.println("Inside check function");
throw new ArithmeticException("demo");
}

public static void main(String args[])


{
try
{
check();
}
catch(ArithmeticException e)
{
System.out.println("caught" + e);
}
}
}

Inside check function

pg. 4 Amol Shinde


EXCEPTION IN JAVA

caughtjava.lang.ArithmeticException: demo

Difference between throw and throws


throw throws
throw keyword is used to throw an throws keyword is used to declare an
exception explicitly. exception possible during its execution.
throw keyword is followed by an instance of throws keyword is followed by one or more
Throwable class or one of its sub-classes. Exception class names separated by commas.
throw keyword is declared inside a method throws keyword is used with method signature
body. (method declaration).
We cannot throw multiple exceptions using We can declare multiple exceptions
throw keyword. (separated by commas) using throws keyword.

finally clause
A finally keyword is used to create a block of code that follows a try block. A finally block of
code is always executed whether an exception has occurred or not.
Example demonstrating finally Clause
Class ExceptionTest
{
public static void main(String[] args)
{
int a[]= new int[2];
System.out.println("out of try");
try
{
System.out.println("Access invalid element"+ a[3]);
/* the above statement will throw ArrayIndexOutOfBoundException */
}
finally
{
System.out.println("finally is always executed.");
}
}
}

pg. 5 Amol Shinde


EXCEPTION IN JAVA

Out of try

finally is always executed.

Exception in thread main java. Lang. exception array Index out of bound exception.

pg. 6 Amol Shinde

You might also like