0% found this document useful (0 votes)
7 views3 pages

Java Exception Handling Guide

Exception handling in Java allows developers to manage runtime errors without crashing the program, utilizing keywords like try, catch, throw, throws, and finally. Exceptions are categorized into checked and unchecked types, with custom exceptions enhancing error management. Proper handling is crucial for maintaining user experience, especially in GUI applications.

Uploaded by

raunaq25244
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)
7 views3 pages

Java Exception Handling Guide

Exception handling in Java allows developers to manage runtime errors without crashing the program, utilizing keywords like try, catch, throw, throws, and finally. Exceptions are categorized into checked and unchecked types, with custom exceptions enhancing error management. Proper handling is crucial for maintaining user experience, especially in GUI applications.

Uploaded by

raunaq25244
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 is a powerful mechanism in Java that allows developers to manage runtime errors

gracefully without crashing the program. These lecture notes cover definitions, mechanisms, code examples,
and diagrams for classroom teaching.

An exception is an object that represents an unusual event or error during program execution. Java uses
exception handling to detect and respond to such events dynamically.
Common causes of exceptions:
• Division by zero
• Accessing invalid array index
• File not found
• Null pointer access

try {
int result = a / b;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero!");
}

Java provides five main keywords for handling exceptions:


• try – contains risky code
• catch – handles exceptions
• throw – used to throw exceptions
• throws – declares possible exceptions
• finally – executes cleanup code

try {
int data = 50 / 0; // risky code
} catch (ArithmeticException e) {
[Link]("Error: Division by zero!");
} finally {
[Link]("Finally block executed.");
}

Exceptions in Java are divided into two main types:


• Checked Exceptions – Must be caught or declared in throws clause (e.g., IOException).
• Unchecked Exceptions – Occur during runtime (e.g., NullPointerException, ArithmeticException).

Object
■■■ Throwable
■■■ Error
■■■ Exception
■■■ IOException (Checked)
■■■ RuntimeException (Unchecked)

When a method might throw an exception, it must declare it using the throws clause.
public void readFile() throws IOException {
FileReader fr = new FileReader("[Link]");
}
To manually raise an exception, use the throw statement:
if (b == 0)
throw new ArithmeticException("Division by zero not allowed.");

Developers can create their own exception classes by extending Exception.


class DivideByZeroException extends Exception {
public DivideByZeroException(String message) {
super(message);
}
}
try {
if (denominator == 0)
throw new DivideByZeroException("Custom: Cannot divide by zero.");
} catch (DivideByZeroException e) {
[Link]([Link]());
}

A try block can have multiple catch blocks. The finally block always executes, whether or not an exception
occurs.
try {
int[] arr = new int[3];
arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Invalid index access.");
} catch (Exception e) {
[Link]("General exception: " + e);
} finally {
[Link]("Program completed.");
}

The following diagram represents the basic flow of try–catch–finally:


■■■■■■■■■■■■■■
■ try block ■
■■■■■■■■■■■■■■
■ Exception?

■■■■■■■■■■■■■■■■■
■ Yes ■ No
▼ ▼
catch block finally block

finally block

A calculator program performs operations like addition, subtraction, etc. If the user inputs an unknown
operator, a custom UnknownOpException is thrown.
class UnknownOpException extends Exception {
public UnknownOpException(String message) {
super(message);
}
}
try {
if (!"+-*/".contains(op))
throw new UnknownOpException("Invalid operator: " + op);
} catch (UnknownOpException e) {
[Link]([Link]());
}

In GUI-based programs, exceptions don’t always terminate the application. However, proper handling is still
crucial for user experience.
try {
Color c = [Link](userInput);
} catch (NumberFormatException e) {
[Link](null, "Invalid color format!");
}

• Exceptions are objects signaling runtime problems.


• try–catch–finally handle exceptional cases effectively.
• Checked exceptions must be declared or caught.
• Unchecked exceptions usually indicate logic errors.
• finally block always executes.
• Custom exceptions improve clarity and error management.

You might also like