Java_Exceptions_PDF
Java_Exceptions_PDF
Exceptions in Java are errors that happen during runtime (program execution) which interrupt the
normal ow of the program.
Java handles exceptions using a powerful Exception Handling mechanism to avoid program
crashes and manage errors gracefully.
All exceptions come from the Throwable class. It has two main types:
Built-in Exceptions:
These are exceptions created by the programmer for custom error handling.
To create one:
Example:
This program throws a custom exception if the user's age is less than 18.
try {
checkAge(age);
} catch (InvalidAgeException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Output: