Attachment (2)
Attachment (2)
Chapter 3 - K Scheme
Exception Handling & Multi-threading
Notes
Errors are severe issues that occur outside the control of the program. They usually indicate
problems related to the system or environment where the application is running. Since these
issues are critical, they are not meant to be handled within the code.
✅ Characteristics of Errors:
Exceptions are events that occur during program execution that disrupt the normal flow of
instructions. They are caused by logical errors in the code and can be handled to ensure the
program continues running smoothly.
✅ Characteristics of Exceptions:
System.out.println(a);
Division by zero
Accessing an invalid array index
Dereferencing a null object
System.out.println(num);
Exception Handling
Exception handling is a mechanism in Java used to manage errors that occur during program
execution. It ensures that the program continues running smoothly even when unexpected
situations arise.
What is an Exception?
1. try-catch Block
try {
} catch (ExceptionType e) {
// Handling code
✅ Example of try-catch:
try {
} catch (ArithmeticException e) {
System.out.println("Program continues...");
Output:
Program continues...
If multiple exceptions can occur, each can be handled in separate catch blocks.
try {
System.out.println(numbers[5]); // ArrayIndexOutOfBoundsException
} catch (ArithmeticException e) {
} catch (ArrayIndexOutOfBoundsException e) {
Output:
3. finally Block
Syntax:
try {
} catch (ExceptionType e) {
} finally {
try {
} catch (ArithmeticException e) {
} finally {
Output:
4. throw Keyword
Syntax:
System.out.println("Access granted.");
Output:
5. throws Keyword
The throws keyword is used in the method signature to declare exceptions that the
method might throw.
Syntax:
import java.io.*;
try {
new ThrowsExample().readFile();
} catch (IOException e) {
Built-in Exception
Java provides a rich set of built-in exceptions to handle common runtime errors. These
exceptions are part of the java.lang package, which is automatically imported in every Java
program.
Throwable
├── Exception
│ ├── IOException
│ ├── SQLException
│ ├── RuntimeException
│ ├── ArithmeticException
│ ├── NullPointerException
│ ├── ArrayIndexOutOfBoundsException
│ ├── IllegalArgumentException
│ └── NumberFormatException
└── Error
├── OutOfMemoryError
├── StackOverflowError
└── VirtualMachineError
Exception Description
import java.io.*;
try {
} catch (FileNotFoundException e) {
Output:
File not found: file.txt (The system cannot find the file specified)
Exception Description
1. ArithmeticException Example:
System.out.println(num);
In this method, you create a class that extends the Thread class and override its run() method.
try {
} catch (InterruptedException e) {
System.out.println(e.getMessage());
Output:
This method is preferred in Java because it avoids the limitation of extending only one class.
try {
} catch (InterruptedException e) {
System.out.println(e.getMessage());
Output:
Method Description
join() Forces the current thread to wait for another thread to finish
Pauses the current thread to allow other threads of the same priority to
yield()
execute
getName() /
Gets/Sets the thread's name
setName()
try {
} catch (InterruptedException e) {
System.out.println(e.getMessage());
t1.start();
try {
} catch (InterruptedException e) {
System.out.println(e.getMessage());
t2.start();
Output:
Thread Synchronization
Synchronization prevents multiple threads from accessing the same resource simultaneously,
ensuring data consistency.
class Counter {
count++;
counter.increment();
});
counter.increment();
});
t1.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
Output:
Without synchronization, the count may result in incorrect values due to thread interference.
Advantages of Multithreading
Disadvantages of Multithreading
✅ Gaming applications.
✅ Real-time systems like GPS or navigation apps.
✅ GUI-based applications where background tasks must run smoothly.
✅ Server-based applications handling multiple client requests.
Deadlock in Java
A deadlock is a situation in which two or more threads are blocked forever, each waiting for
the other to release a resource. It typically occurs when multiple threads hold some shared
resources and try to acquire the resources that are held by other threads.
try {
} catch (InterruptedException e) {
e.printStackTrace();
try {
} catch (InterruptedException e) {
e.printStackTrace();
t1.start();
t2.start();
Output:
Thread 1 is executing methodA