OOPs Using Java Notes UNIT-2
OOPs Using Java Notes UNIT-2
Exception handling is a mechanism to handle runtime errors so that the normal flow of
the application can be maintained. It allows us to handle exceptional conditions in a
controlled way.
Exceptions are events that occur during the execution of a program that disrupt the
normal flow of instructions. Errors are mistakes in the code that can be caught and
handled.
Types of Exception
1. Checked Exceptions: These are exceptions that are checked at compile-time. Examples
include IOException, SQLException, etc.
2. Unchecked Exceptions: These are exceptions that are not checked at compile-time.
Examples include ArithmeticException, NullPointerException, etc.
When an exception occurs, the control flow is transferred to the catch block. If there is
no catch block, the control flow is transferred to the default exception handler.
When an exception occurs, the JVM searches for a catch block to handle the exception.
If it finds one, it transfers the control flow to the catch block. If it doesn't find one, it
terminates the program.
try: The try block contains the code that may throw an exception.
catch: The catch block contains the code to handle the exception.
finally: The finally block contains the code that is executed regardless of whether an
exception occurs or not.
throw: The throw keyword is used to throw an exception explicitly.
throws: The throws keyword is used to declare that a method may throw an exception.
try {
} catch (ArithmeticException e) {
} finally {
In-built exceptions are exceptions that are already defined in Java, such as
ArithmeticException, NullPointerException, etc. User-defined exceptions are exceptions
that are defined by the user, such as custom exceptions for a specific application.
try {
validateAge(15); // This will throw a UserDefinedException
} catch (UserDefinedException e) {
super(message);
try {
readFile("non-existent-file.txt"); // This will throw a Checked Exception
(IOException)
} catch (IOException e) {
try {
} catch (ArithmeticException e) {
//...
}
Input/Output Basics: Byte Streams and Character Streams
In Java, input/output operations are performed using streams. There are two types of
streams:
1. Byte Streams: These streams deal with bytes, which are the basic units of data in
computers. They are used to read and write binary data, such as images and audio files.
Examples of byte streams include FileInputStream and FileOutputStream.
2. Character Streams: These streams deal with characters, which are the basic units of text
data. They are used to read and write text data, such as strings and characters. Examples
of character streams include FileReader and FileWriter.
Byte Streams
Here's an example program that demonstrates the use of byte streams to read and write
a file:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
try {
fos.write(buffer, 0, bytesRead);
fis.close();
fos.close();
} catch (IOException e) {
Character Streams
Here's an example program that demonstrates the use of character streams to read and
write a file:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
try {
fw.write(buffer, 0, charsRead);
fr.close();
fw.close();
} catch (IOException e) {
Here's an example program that demonstrates how to read and write a file in Java using
character streams:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReadWriteFileExample {
try {
// Write to a file
fw.write("Hello, World!");
fw.close();
System.out.println("Wrote to file");
fr.close();
} catch (IOException e) {
}
Multithreading: Thread
In Java, a thread is a separate path of execution that can run concurrently with other
threads. Threads are used to improve the responsiveness and performance of a
program.
Creating Threads
1. Extending the Thread class: By extending the Thread class, you can create a new
thread by overriding the run() method.
2. Implementing the Runnable interface: By implementing the Runnable interface, you
can create a new thread by providing a run() method.
Here's an example program that demonstrates creating threads using both methods:
myThread.start();
thread.start();
Thread Priorities
Thread priorities are used to determine the order in which threads are executed. In Java,
threads have a priority between 1 and 10, with 10 being the highest priority.
thread1.setPriority(5);
thread1.start();
thread2.start();
Synchronizing Threads
Synchronization is used to ensure that only one thread can access a shared resource at a
time. In Java, synchronization is achieved using the synchronized keyword.
count++;
increment();
}
public class SynchronizingThreads {
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
Inter-thread Communication
isReady = true;
notify();
synchronized (this) {
while (!isReady) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("Thread is ready!");
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
myRunnable.setReady();