Object Oriented
Programming with Java
Unit-2:
2: Exception Handling,
I/O, and Multithreading
ntroduction to Exception Handling
An exception is an event that disrupts the normal flow of a program.
Java’s exception mechanism helps manage runtime errors systematically.
Exceptions allow separation of error-handling
handling logic from regular logic.
Exception vs. Error
Exception: Recoverable conditions (e.g., IOException, NullPointerException)
Error: Serious problems not intended to be caught (e.g., OutOfMemoryError,
StackOverflowError)
Types of Exceptions
Checked Exceptions: Checked at compile time (e.g., IOException, SQLException
Unchecked Exceptions: Occur at runtime (e.g., NullPointerException,
ArithmeticException)
Errors: Unrecoverable issues
Control Flow in Exception Handling
[Link] block is executed
[Link]
If an exception occurs, control jumps to the matching catch block
[Link]
Finally block runs regardless of exception
JVM Reaction to Uncaught Exceptions
•If exceptions are not caught:
• JVM prints stack trace
• Program terminates abnormally
Example:
int result = 10 / 0; // ArithmeticException
try, catch, finally
•try:
try: Block where code is tested for exceptions
•catch: Block that handles the exception
•finally:
finally: Executes regardless of an exception (used to close resources)
Example:
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Divide by zero!");
} finally {
[Link]("End of block");
}
throw and throws
•throw:
throw: Used to explicitly throw an exception
•throws:
throws: Declares exceptions that a method might throw
Example:
void check(int age) throws ArithmeticException {
if(age < 18) throw new ArithmeticException("Not
ArithmeticException eligible");
}
In-built vs. User-defined Exceptions
•In-built Exceptions: Provided by Java (NullPointerException
NullPointerException,
ArrayIndexOutOfBoundsException)
•User-defined:
class MyException extends Exception {
MyException(String message) {
super(message);
}
}
Checked vs Unchecked Exceptions
eature Checked Unchecked
Compile-time checking Yes No
Base Class Exception RuntimeException
IOException, NullPointerException,
xamples
SQLException ArithmeticException
Input/Output Basics
Java I/O Overview
•Java uses streams to perform input/output operations
•Two types:
• Byte Streams – Handle binary data
• Character Streams – Handle text data
Byte Streams
•Classes: InputStream, OutputStream, FileInputStream,
FileInputStream FileOutputStream
•Used for binary files like images, audio
Example:
FileInputStream fis = new FileInputStream("[Link]");
FileInputStream
nt i = [Link]();
Character Streams
•Classes: Reader, Writer, FileReader, FileWriter
•Used for reading/writing text
Example:
FileWriter fw = new FileWriter("[Link]");
("[Link]");
[Link]("Hello Java");
[Link]();
Reading/Writing a File
•Read: FileReader, BufferedReader
•Write: FileWriter, BufferedWriter
Example:
BufferedReader br = new BufferedReader(new
BufferedReader FileReader("[Link]"));
String line = [Link]();
Multithreading in Java
What is a Thread?
•A thread is a lightweight process
•Multiple
Multiple threads can run concurrently in the same program
Thread Life Cycle
[Link]
[Link]
[Link]
[Link]/Waiting
[Link]
Creating Threads
•By Extending Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread
("Thread running");
}
}
By Implementing Runnable
class MyRunnable implements Runnable {
public void run() {
[Link]("Thread
("Thread running");
}
}
Thread Priorities
Threads have priorities from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY)
Default is 5 (NORM_PRIORITY)
Methods:
setPriority()
getPriority()
Synchronizing Threads
•Prevents
Prevents thread interference and data inconsistency
•Use the synchronized keyword
Example:
synchronized void printTable(int n) {
// synchronized block
}
Inter-thread Communication
•Threads can communicate using:
•wait()
•notify()
•notifyAll()
Used for: Coordinating threads in producer-consumer
producer problems
Conclusion and Summary
•Exception Handling:: Makes code robust and error-tolerant
error
•I/O in Java:: Uses streams for efficient data processing
•Multithreading:: Enables concurrent programming and better performance