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

Java Exception Handling Guide

The document explains exception handling in Java, defining exceptions as runtime errors and outlining the five keywords used: try, catch, throw, throws, and finally. It categorizes exceptions into checked, unchecked, and errors, providing examples for each type and demonstrating syntax for handling exceptions with multiple catch blocks. Additionally, it covers user-defined exceptions and the use of the throw and throws keywords for managing exceptions in Java code.
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)
18 views6 pages

Java Exception Handling Guide

The document explains exception handling in Java, defining exceptions as runtime errors and outlining the five keywords used: try, catch, throw, throws, and finally. It categorizes exceptions into checked, unchecked, and errors, providing examples for each type and demonstrating syntax for handling exceptions with multiple catch blocks. Additionally, it covers user-defined exceptions and the use of the throw and throws keywords for managing exceptions in Java code.
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

Exception Handling

An exception is an abnormal condition that arises in code sequence at run time. In other words
exception is a runtime error.
A Java exception is an object that describes an exceptional condition that has occurred in a piece
of code.
Java’s exception handling avoids these problems and, in the process, brings run-time error
management into the object oriented world.

Java exception handling is managed via five keywords:


1. try
2. catch
3. throw
4. throws
5. finally

java Syntax for Exception Handling

try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
finally
{
// block of code to be executed after try block ends
}
Class Hierarchy of Exception

Types of Exception:
1) Checked Exception

The classes that extend Throwable class except RuntimeException and Error are known as
checked exceptions [Link], SQLException etc. Checked exceptions are checked at
compile-time.

2) Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time rather they are checked at runtime.

3) Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.


class ExceptionExample1
{
public static void void main(String s[])
{
try
{
int a = 5,b;
b = a/0;
[Link](b);

}
catch( ArithmaticException e)
{
[Link](e);
b=a;
[Link](b);
}
}
}

1) Scenario where ArithmeticException occurs


int a=50/0;
2) Scenario where NullPointerException occurs

String s=null;
[Link]([Link]());
3) Scenario where NumberFormatException occurs
String s="abc";
int i=[Link](s);
4) Scenario where ArrayIndexOutOfBoundsException occurs
int a[]=new int[5];
a[10]=50;
Multiple Catch Block:
class ExceptionExample3
{ public static void main(String s[])
{
try
{
int a[] = {1,2,3,4};
int b;
b = a[4]/0;
[Link](b);
}
catch( ArithmeticException e)
{
[Link](“calculation error”);
}
catch( ArrayIndexOutOfBoundsException e)
{
[Link](“range error”);
}
}}

Throw Keyword

The throw keyword in Java is used to explicitly throw an exception from a method or any block
of code. We can throw either checked or unchecked exception. The throw keyword is mainly
used to throw custom exceptions.

class ExceptionExample4
{
public void checkExecption()
{
try
{
throw new ArithmeticException(“some message”);
}
catch( ArithmeticException e)
{
[Link](e);
}
}
}
Throws

The throws keyword in Java is used to handover the task of handling checked exception to
JVM. JVM does not handle checked exception by default. Try – catch can be also used in
place of throws keyword for checking and handling.

class ExceptionExample5
{
public void checkExecption() throws IOException
{
throw new IOException(“my io exception”);

}
}

Finally

finally creates a block of code that will be executed after a try/catch block The finally block
will execute whether or not an exception is thrown. If an exception is thrown, the finally
block will execute even if no catch statement matches the exception. This can be useful for
closing file handles and freeing up any other resources.

class ExceptionExample6
{
public void checkExecption()
{
try
{
throw new IOException(“my io exception”)
}
catch( ArithmaticException e)
{
[Link](e);
}
catch(NullPointerException e)
{
[Link](e);
}
finally
{
[Link](“finally block execute”);
}
}
}
Creating User Defined Exception:

class MyException extends Exception // to create a exception class inherit Exception class
{
public MyException(String msg)
{
super(msg);
}
}

class CheckMyException
{
public void checkRange(int x) throws MyException
{
if(x>=4 && x<=20)
throw new MyException("out of range of 4 and 20");
else
[Link]("value is in range");
}
}

class CallMyException
{
public static void main(String args[]) throws MyException
{
CheckMyException obj = new CheckMyException();
[Link](15);
[Link](21);
[Link](7);
}
}

You might also like