Java Unit 2 - Complete Notes
1. Exception Handling (अपवाद प्रबंधन)
1.1 The Idea Behind Exception (अपवाद का विचार)
Definition (English): An exception is an event that disrupts the normal flow of the program
during execution. Java provides a robust mechanism to handle such scenarios using
Exception Handling.
परिभाषा (हिंदी): अपवाद एक ऐसा इवेंट होता है जो प्रोग्राम के सामान्य
प्रवाह को बाधित करता है। Java में इसे नियंत्रित करने के लिए Exception
Handling का उपयोग किया जाता है।
1.2 Exceptions & Errors
• Exceptions can be handled, Errors usually cannot be handled.
• Example of Exception: ArithmeticException
• Example of Error: OutOfMemoryError
1.3 Types of Exceptions
• Checked Exceptions – Handled at compile time. E.g., IOException, SQLException
• Unchecked Exceptions – Occur at runtime. E.g., ArithmeticException, NullPointerException
1.4 Control Flow in Exceptions
When an exception occurs in try block, control is transferred to the appropriate catch block.
After catch, finally block (if present) will execute.
1.5 JVM Reaction to Exceptions
If exceptions are not handled, JVM halts the program and prints a stack trace.
1.6 Use of try, catch, finally, throw, throws
• try: Code that may throw exception.
• catch: Handles the exception.
• finally: Executes regardless of exception.
• throw: Used to throw an exception.
• throws: Declares exceptions in method signature.
1.7 In-built and User Defined Exceptions
• In-built: Already available in Java (e.g., ArithmeticException).
• User-defined: Created using custom class extending Exception.
Example: User Defined Exception
class MyException extends Exception {
MyException(String msg) {
super(msg);
}
}
public class Test {
public static void main(String[] args) {
try {
throw new MyException("Custom Error");
} catch (MyException e) {
[Link]([Link]());
}
}
}
2. Input / Output Basics (इनपुट/आउटपुट बेसिक्स)
2.1 Byte Streams and Character Streams
• Byte Streams: For binary data (FileInputStream, FileOutputStream).
• Character Streams: For text data (FileReader, FileWriter).
2.2 Reading and Writing File in Java
import [Link].*;
public class FileExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("[Link]");
[Link]("Hello World");
[Link]();
FileReader reader = new FileReader("[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
}
[Link]();
} catch (IOException e) {
[Link](e);
}
}
}
3. Multithreading (मल्टीथ्रेडिंग)
3.1 Thread
A thread is a lightweight process. Java supports multithreading to allow concurrent
execution.
3.2 Thread Life Cycle
1. New
2. Runnable
3. Running
4. Waiting/Blocked
5. Terminated
3.3 Creating Threads
• By extending Thread class
• By implementing Runnable interface
class MyThread extends Thread {
public void run() {
[Link]("Thread running");
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
3.4 Thread Priorities
Thread priorities decide the order of execution. Range: 1 (MIN) to 10 (MAX).
3.5 Synchronizing Threads
Synchronization prevents concurrent access issues by allowing one thread at a time to
access a method/block.
class Counter {
int count = 0;
synchronized void increment() {
count++;
}
}
3.6 Inter-thread Communication
Java provides `wait()`, `notify()`, and `notifyAll()` for communication between threads.
synchronized(obj) {
[Link]();
[Link]();
}