1/16
Flashcards on Exception Handling in Java
Exception
An error event that occurs during program execution, preventing normal continuation.
Exception Handling
Allows a program to manage exceptions and recover from errors.
Common causes of exceptions in Java
Numeric overflow, type conversion, dividing by zero, file not found, running out of memory, SQL errors, array bounds, null object.
Approaches to Error Checking
Handle errors appropriately for every error, do normal processing in one place and handle errors in another.
Important methods of the Exception class
printStackTrace(), getMessage(), getLocalizedMessage(), toString()
java.lang.Exception
All exceptions are derived from this class.
Throwing an exception
The creation of an object representing an error.
Handling an exception
The code that detects and manages the exception.
Common Exception Types
NullPointerException, ClassCastException, ArithmeticException, NumberFormatException, IOException, FileNotFoundException, EOFException.
Try-Catch Structure
A control structure used to separate normal code from error-handling code.
Components of Try-Catch Structure
try, catch, finally
Ordering Catch Statements
The order in which catch statements should be arranged from most specific to most general.
Checked Exception
An exception the compiler can recognize, requiring a throws statement.
Unchecked Exception
An exception the compiler cannot determine at compile time, not requiring a throws statement.
Throw statement
Used to create an exception in your program.
Throws statement
A method throws exceptions using this statement.
When to throw an exception in your code
When an object of the class or class methods aren’t used properly, when a method encounters a situation where it’s unable to complete its task, when you want to generate an exception to test an exception handler.