0% found this document useful (0 votes)
34 views2 pages

Understanding Java Exceptions and Handling

An exception is an event that disrupts normal program execution, caused by errors, resource failures, or unexpected input. Java exceptions are categorized into checked and unchecked exceptions, all deriving from the Throwable class. Exception handling in Java uses keywords like try, catch, finally, throw, and throws to manage errors and ensure proper resource cleanup.

Uploaded by

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

Understanding Java Exceptions and Handling

An exception is an event that disrupts normal program execution, caused by errors, resource failures, or unexpected input. Java exceptions are categorized into checked and unchecked exceptions, all deriving from the Throwable class. Exception handling in Java uses keywords like try, catch, finally, throw, and throws to manage errors and ensure proper resource cleanup.

Uploaded by

renuajith39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What is an Exception?

An exception is an event that disrupts normal execution during a program's runtime �. Exceptions can
be generated by the Java runtime system or manually in code. Common reasons for exceptions include
programmer errors, resource failure, or unexpected input�.

Exception Types

Java exceptions fall into two categories:

Checked exceptions: Checked at compile time.

Unchecked exceptions: Checked at runtime, including RuntimeException and its subclasses.

Exception Class Hierarchy

All exceptions derive from the Throwable class.

The hierarchy is:

 ThrowableException (checked exceptions)


 Error (serious system errors, typically not handled).

Keywords Used in Java Exception Handling

 try: Contains code that might throw exceptions.


 catch: Handles specific exceptions thrown in the try block.
 finally: Executes cleanup code, always runs after try/catch, regardless of exception occurrence.
 throw: Used to manually throw an exception.
 throws: Indicates which exceptions a method might throw and must be handled elsewhere.

Basic Syntax and Examples

Try-Catch Block

try {

// risky code

catch (ExceptionType name) {

// code to handle exception

Example:

try {

int divideByZero = 5 / 0;

}
catch (ArithmeticException e) {

[Link]("ArithmeticException => " + [Link]());

In this example, division by zero triggers the ArithmeticException, which is then caught and handled.

Finally Block

try {

// code

} catch (Exception e) {

// handle exception

} finally {

// cleanup code

The finally block executes whether an exception occurs or not, ensuring resources are released.

Throw and Throws

 throw is used for manually creating exceptions.


 throws signals that a method may throw certain exceptions, which must be handled by the
caller.

Example:

public void readFile() throws IOException {

// code that may throw IOException

Exception Methods

 getMessage(): Returns detailed exception message.


 getCause(): Returns cause of exception as Throwable object.
 toString(): Returns class name and exception description.
 printStackTrace(): Prints exception's stack trace.

Exception Handling Flow

 The JVM checks if the exception is handled.


 If handled, normal flow resumes.
 If not, default handler prints stack trace and terminates program.

You might also like