How many types of checked exceptions are there in Java?
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.
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
.
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:
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.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());
}
}
}
Example:
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.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());
}
}
}
Example:
RuntimeException: These are a type of unchecked exception and can be thrown anywhere in your program.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());
}
}
}
Example:
Exception: This is the parent class for all exceptions in Java.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());
}
}
}
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 ExceptionEach type has its own use and is used to handle different situations.