0% found this document useful (0 votes)
3 views

Exceptions in Java

In Java, exceptions are categorized into checked exceptions, which are verified at compile-time, and unchecked exceptions, which occur due to logical errors. Checked exceptions include IOException and SQLException, while unchecked exceptions include NullPointerException and ArithmeticException. Additionally, errors represent serious issues that applications cannot recover from, such as OutOfMemoryError and StackOverflowError.

Uploaded by

anyafranando
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)
3 views

Exceptions in Java

In Java, exceptions are categorized into checked exceptions, which are verified at compile-time, and unchecked exceptions, which occur due to logical errors. Checked exceptions include IOException and SQLException, while unchecked exceptions include NullPointerException and ArithmeticException. Additionally, errors represent serious issues that applications cannot recover from, such as OutOfMemoryError and StackOverflowError.

Uploaded by

anyafranando
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/ 5

Exceptions in Java

In Java, exceptions are used to handle errors and other exceptional events in a program.
They are categorized into two main types: checked exceptions and unchecked
exceptions. Below are the main exception types and their key details

1. Checked Exceptions

Checked exceptions are checked at compile-time, meaning the compiler ensures that the
exception is either caught or declared to be thrown.

Examples:

• IOException: Thrown when an input/output operation fails.

o Example: File not found, unable to read a file.

• SQLException: Thrown when there's an error interacting with a database.

o Example: Invalid SQL query, database connection failure.


• ClassNotFoundException: Thrown when a class is not found at runtime.

o Example: Trying to load a class that does not exist.

• FileNotFoundException: Thrown when a specified file is not found.

o Example: Opening a file that doesn't exist.


2. Unchecked Exceptions

Unchecked exceptions are not checked at compile-time, and they occur due to logical
errors in the program. These exceptions inherit from RuntimeException.

Examples:

• NullPointerException: Thrown when attempting to access a null object reference.

• ArrayIndexOutOfBoundsException: Thrown when trying to access an array


element with an invalid index.
• ArithmeticException: Thrown when an arithmetic operation goes wrong (e.g.,
division by zero).

• IllegalArgumentException: Thrown when an invalid argument is passed to a


method.

• NumberFormatException: Thrown when trying to convert a string to a numeric type


but the string is invalid.
• ClassCastException: Thrown when trying to cast an object to a subclass it doesn't
belong to.

3. Errors

Errors are serious issues that applications typically cannot recover from. These are not
exceptions but are part of the java.lang.Error class.

Examples:

• OutOfMemoryError: Thrown when the Java Virtual Machine (JVM) runs out of
memory.

• StackOverflowError: Thrown when a program recurses too deeply, exhausting the


stack memory.

• VirtualMachineError: Thrown when the JVM encounters a critical problem.

• NoClassDefFoundError: Thrown when a class required for execution is not found at


runtime.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy