JAVA EXCEPTION HANDLING MASTERCLASS
Complete Guide to Try-Catch, Runtime Exceptions, Throw and Throws
1. Exception Handling Fundamentals
An Exception is an unwanted or unexpected event, which occurs during the execution of a program
(at runtime) and disrupts the normal flow of the program's instructions.
Try-Catch-Finally Blocks
Try: The "try" block is used to enclose the code that might throw an exception.
Catch: The "catch" block is used to handle the exception. It must follow the try block.
Finally: The "finally" block is used to execute important code such as closing connections. It executes
whether an exception is handled or not.
public class TryCatchExample {
public static void main(String args[]) {
try {
int data = 100 / 0; // Throws ArithmeticException
} catch (ArithmeticException e) {
[Link]("Exception: Cannot divide by zero.");
} finally {
[Link]("Finally: Execution completed.");
}
}
}
Output:
Exception: Cannot divide by zero.
Finally: Execution completed.
Page 1
2. Run-time Exceptions (Unchecked)
Run-time exceptions are exceptions that occur at the time of execution. These are also called
Unchecked Exceptions because the compiler does not check them at compile-time.
Common examples include:
• ArithmeticException: Mathematical errors (division by zero).
• NullPointerException: Accessing a null object.
• ArrayIndexOutOfBoundsException: Accessing an invalid array index.
public class RuntimeDemo {
public static void main(String[] args) {
String str = null;
try {
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Runtime Error: Caught a Null Pointer!");
}
}
}
Output:
Runtime Error: Caught a Null Pointer!
Page 2
3. The 'throw' Keyword
The throw keyword is used to explicitly throw an exception from a method or any block of code. It is
mainly used to throw custom exceptions.
public class ThrowExample {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - Under 18");
} else {
[Link]("Access granted!");
}
}
public static void main(String[] args) {
try {
checkAge(15);
} catch (Exception e) {
[Link]("Caught: " + [Link]());
}
}
}
Output:
Caught: Access denied - Under 18
Page 3
4. The 'throws' Keyword
The throws keyword is used in the method signature to declare that the method might throw one or
more exceptions. It "delegates" the responsibility of handling the exception to the caller of the
method.
import [Link];
public class ThrowsExample {
// Declaring that this method might throw an exception
static void findFile() throws IOException {
throw new IOException("File not found on disk");
}
public static void main(String[] args) {
try {
findFile();
} catch (IOException e) {
[Link]("Main Handled: " + [Link]());
}
}
}
Output:
Main Handled: File not found on disk
Page 4
5. Comparison: Throw vs Throws
Feature Throw Throws
Usage To throw an exception explicitly. To declare an exception in method signature.
Placement Inside the method body. With the method signature.
Number Followed by an instance (new). Followed by class names.
Page 5