0% found this document useful (0 votes)
2 views14 pages

ErrorHandling.pptx

The document provides an overview of Java error handling, distinguishing between errors and exceptions. It explains that errors are serious issues typically related to the Java Runtime Environment, while exceptions can be caught and handled within the code. The document also details checked and unchecked exceptions, error handling practices, and lists various types of errors and exceptions in Java with their descriptions.

Uploaded by

shalaeestoche
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

ErrorHandling.pptx

The document provides an overview of Java error handling, distinguishing between errors and exceptions. It explains that errors are serious issues typically related to the Java Runtime Environment, while exceptions can be caught and handled within the code. The document also details checked and unchecked exceptions, error handling practices, and lists various types of errors and exceptions in Java with their descriptions.

Uploaded by

shalaeestoche
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Error Handling

for subject in Computer Programming 2 (CC103)

Subject Teacher: Mr. Jonathan Pastor


Java Error Handling

When executing Java code, different errors can


occur: coding errors made by the programmer, errors
due to wrong input, or other unforeseeable things.

When an error occurs, Java will normally stop and


generate an error message. The technical term for
this is: Java will throw an exception (throw an
error).
Errors vs Exceptions
Errors:
These represent serious problems that a typical
application should not attempt to handle. They
usually indicate issues with the Java Runtime
Environment (JRE) or the system itself.

Examples include OutOfMemoryError (when the JVM runs


out of memory) and StackOverflowError (when the call
stack overflows). Errors are generally
unrecoverable, and the application will likely
terminate.
Errors vs Exceptions

Exceptions:
These indicate conditions that a program might try
to catch and handle. They represent problems that
can occur during the execution of the program, often
due to issues within the code itself or external
factors. Exceptions are categorized into checked and
unchecked.
Exceptions:

Checked exceptions:

These exceptions must be either caught using a


try-catch block or declared in the method signature
using the throws keyword. Examples include
IOException and SQLException. The compiler enforces
this handling, ensuring that potential issues are
addressed.
Exceptions:
Unchecked exceptions:

Also known as runtime exceptions, these do not need


to be explicitly caught or declared. They typically
result from programming errors, such as
NullPointerException (accessing a null reference) or
ArrayIndexOutOfBoundsException (accessing an array
element outside its bounds). While not mandatory,
handling unchecked exceptions can improve program
robustness.
Error Handling

Don't catch Errors. Handle Exceptions.


Error Handling

try {
// Code that might throw an exception
}

catch (ExceptionType name) {


// Code to handle the exception
}
Error Handling
The throw statement allows you to create a custom
error. It can be used together with an exception type.

if (age < 18) {


throw new ArithmeticException("Access denied -
You must be at least 18 years old.");
}
else {
System.out.println("Access granted - You are old
enough!");
}
}
Error Handling

• The finally statement lets you execute code, after


try...catch, regardless of the result.
• You can handle multiple errors by having multiple
catch statements.
• Not required but it’s a good practice to add code
inside a catch block.
Error Handling
Here is the list of Error and Exception types in
Java:

Error/Exception Description
ArithmeticError Occurs when a numeric calculation goes wrong
ArrayIndexOutOfBoundsException Occurs when trying to access an index number that does not exist in an array
ClassFormatError Occurs when a class file cannot be accessed
ClassNotFoundException Occurs when trying to access a class that does not exist
ConcurrentModificationException Occurs when an element is added or removed from iterables

FileNotFoundException Occurs when a file cannot be accessed


IncompatibleClassChangeError Occurs when there's been a change in a base class after a child class has already been
initialized
InputMismatchException Occurs when entering wrong input (e.g. text in a numerical input)
Error Handling
Here is the list of Error and Exception types in
Java:

Error/Exception Description
InterruptedException Occurs when a Thread is interrupted while waiting/sleeping
InvalidClassException Occurs when the Serialization runtime observes a problem with a class
IOException Occurs when an input or output operation fails
NegativeArraySizeException Occurs when trying to create an array with negative size
NoClassDefFoundError Occurs when the class is not found at runtime
NoSuchFieldException Occurs when trying to access a class field/variable that does not exist
NoSuchMethodException Occurs when trying to access a class method that does not exist

InterruptedException Occurs when a Thread is interrupted while waiting/sleeping


Error Handling
Here is the list of Error and Exception types in
Java:

Error/Exception Description
InvalidClassException Occurs when the Serialization runtime observes a problem with a class
IOException Occurs when an input or output operation fails
NegativeArraySizeException Occurs when trying to create an array with negative size
NoClassDefFoundError Occurs when the class is not found at runtime
NoSuchFieldException Occurs when trying to access a class field/variable that does not exist
NoSuchMethodException Occurs when trying to access a class method that does not exist
NullPointerException Occurs when trying to access an object referece that is null

NumberFormatException Occurs when it is not possible to convert a specified string to a numeric type
Error Handling
Here is the list of Error and Exception types in
Java:

Error/Exception Description
RuntimeException Occurs when an exception occurs at runtime
StringIndexOutOfBoundsException Occurs when trying to access a character in a String that does not exist
TypeNotPresentException Occurs when a type cannot be found
IllegalArgumentException Occurs when when an illegal argument is passed to a method
IllegalStateException Occurs when when a method is called at an illegal time
RuntimeException Occurs when an exception occurs at runtime
StringIndexOutOfBoundsException Occurs when trying to access a character in a String that does not exist

TypeNotPresentException Occurs when a type cannot be found

You might also like