Exception Mastery: Secrets of Handling, Customizing, and Keywords
Learning Outcome
4
Create and use custom exceptions for application-specific error handling.
3
Differentiate between throw and throws keywords.
2
Learn how to use try, catch, and finally blocks to handle runtime errors.
1
Understand why exception handling is necessary in Java.
Control statements – if, else, loops
OOP concepts – class, object, inheritance
Compile-time vs Run-time errors
Types of exceptions – Checked & Unchecke
What is Exception Handling?
A mechanism used to handle runtime errors so that the normal flow of the program is not interrupted. Without it, the program crashes abruptly.
Try - The Moniter:
Contains code that may cause an exception.
Catch - The Handler:
Handles the exception if it occurs, preventing a crash.
Finally - The Cleanup:
Always executes, regardless of errors. Used for closing resources.
Syntax Structure
try {
// Code that may cause an exception
// e.g., opening a file, dividing by zero }
catch (ExceptionType e) {
// Code to handle the exception
// e.g., print error, log message }
finally {
// Optional block
// Code that always executes
public class Test {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b; // Exception occurs here
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("End of program");
}
}
}
Example
Throw
Throws
Used to explicitly throw an exception instance.
Used to declare potential exceptions.
Followed by an instance of a class.
Followed by class names.
Used inside the method body.
Used with the method signature.
Cannot throw multiple exceptions.
Can declare multiple (comma separated).
class Test {
static void check(int age) throws Exception {
if (age < 18)
throw new Exception("Not eligible");
}
public static void main(String[] args) {
try {
check(16);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Example
Sometimes, the built-in exceptions are not enough, so we create our own exception.
Extend Exception for checked or RuntimeException for unchecked exceptions.
Pass the custom error message to the parent class using super(message).
Use throw to raise the custom exception in your code.
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) {
super(msg);
}
}
class Test {
public static void main(String[] args) throws InvalidAgeException {
int age = 16;
if (age < 18)
throw new InvalidAgeException("Not eligible");
}
}
Example
Summary
5
Custom exceptions are created by extending Exceptions or RuntimeException.
4
throw explicitly raises an exception, throws declares it in the method.
3
finally executes for cleanup.
2
try contains risky code, catch handles exceptions
1
Exception handling prevents program crashes
Quiz
Which of the following keywords is used to declare that a method may throw an exception?
A. throw
B. throws
C. finally
D. try
Which of the following keywords is used to declare that a method may throw an exception?
A. throw
B. throws
C. finally
D. try
Quiz-Answer