How many types of checked exceptions are there in Java?

Gloria 77 Published: 07/06/2024

How many types of checked exceptions are there in Java?

I'm happy to respond in English for you!

In Java, a checked exception is an error that the compiler checks and forces you to handle at runtime using try-catch blocks or methods that declare them as part of their method signature. There are two types of checked exceptions: Exception and its subclasses.

The top-level class Exception is the superclass of all checked exceptions in Java. It represents the base type for all checked exceptions.

RuntimeException (and its subclasses): These are runtime exceptions that occur during the execution of a program. They include errors like NullPointerException, ArrayIndexOutOfBoundsException, and ClassCastException. Since they are unchecked, you don't need to declare them in your method signature. However, when an exception of this type is thrown from within a try block, it will propagate through any subsequent catch blocks until it reaches the topmost level of the program.

Examples: ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, IllegalStateException, and NullPointerException.

Error (and its subclasses): These are errors that occur during the execution of a Java Virtual Machine (JVM). They include severe errors like OutOfMemoryError, StackOverflowError, and LinkageError. Like runtime exceptions, you don't need to declare them in your method signature, but they will still propagate through any subsequent catch blocks until they reach the topmost level.

Examples: AbstractMethodError, AssertionError, linkingError, OutOfMemoryError, and StackOverflowError.

It's essential to understand that not all exceptions are checked. Unchecked exceptions are subclasses of RuntimeException or Error. Java requires you to handle checked exceptions using try-catch blocks, whereas unchecked exceptions can propagate through your program without explicit handling.

In summary, there is only one top-level class Exception, and its direct subclasses are RuntimeException (representing runtime exceptions) and Error (representing errors). This distinction helps ensure that your Java programs are robust, reliable, and maintainable.

types of exception in java with examples

I'd be happy to explain the types of exceptions in Java with examples!

Java provides several types of exceptions that can be thrown by a program. Exceptions are used to handle runtime errors and abnormal conditions.

Here's a list of the main types of exceptions:

Checked Exceptions: These exceptions are checked at compile-time, meaning that you must explicitly catch them or declare them in your method's throws clause. Examples of checked exceptions include IOException, SQLException, and NetworkException.

Example:

    import java.io.*;

public class Test {

public static void main(String[] args) {

try {

File file = new File("test.txt");

FileReader reader = new FileReader(file);

char[] buffer = new char[10];

reader.read(buffer);

System.out.println(new String(buffer));

} catch (IOException e) {

System.out.println("Caught an IO exception: " + e.getMessage());

}

}

}

Unchecked Exceptions: These exceptions are not checked at compile-time and can be thrown anywhere in a program. Examples of unchecked exceptions include ArithmeticException, NullPointerException, and ClassCastException.

Example:

    public class Test {

public static void main(String[] args) {

try {

int i = Integer.parseInt("test");

System.out.println(i);

} catch (NumberFormatException e) {

System.out.println("Caught an NumberFormat exception: " + e.getMessage());

}

}

}

Error Exceptions: These exceptions are thrown by the JVM itself and can't be caught or declared in a method's throws clause. Examples of error exceptions include OutOfMemoryError, StackOverflowError, and SecurityException.

Example:

    public class Test {

public static void main(String[] args) {

try {

int i = 1 / 0; // will throw an ArithmeticException

} catch (ArithmeticException e) {

System.out.println("Caught an Arithmetic exception: " + e.getMessage());

}

}

}

RuntimeException: These are a type of unchecked exception and can be thrown anywhere in your program.

Example:

    public class Test {

public static void main(String[] args) {

try {

throw new RuntimeException("This is a runtime exception");

} catch (Exception e) {

System.out.println("Caught an Exception: " + e.getMessage());

}

}

}

Exception: This is the parent class for all exceptions in Java.

Example:

    public class Test {

public static void main(String[] args) {

try {

throw new Exception("This is an exception");

} catch (Exception e) {

System.out.println("Caught an Exception: " + e.getMessage());

}

}

}

In summary, the types of exceptions in Java are:

Checked Exceptions Unchecked Exceptions Error Exceptions RuntimeExceptions Exception

Each type has its own use and is used to handle different situations.