Types of unchecked exception in Java

Alex 168 Published: 07/30/2024

Types of unchecked exception in Java

I'll respond in English as per your request!

Java is a statically-typed language that is designed to avoid runtime errors by detecting and throwing exceptions at compile-time itself. However, despite these efforts, some situations can still lead to unchecked exceptions being thrown at runtime. Unchecked exceptions are those that are not explicitly declared in the method's throws clause or caught using a try-catch block.

Here are some common types of unchecked exceptions in Java:

ArithmeticException: Thrown when an invalid arithmetic operation is attempted, such as dividing by zero.

Example: int x = 5 / 0; // throws ArithmeticException

ArrayIndexOutOfBoundsException: Thrown when you try to access an element outside the bounds of an array or a subarray.

Example: int[] arr = new int[3]; System.out.println(arr[3]); // throws ArrayIndexOutOfBoundsException

ClassCastException: Thrown when you try to cast an object to a subclass of which it is not an instance of.

Example: Integer i = 5; String s = (String) i; // throws ClassCastException

NullPointerException: Thrown when you try to access or manipulate the state of a null object reference.

Example: String s = null; System.out.println(s.length()); // throws NullPointerException

StringIndexOutOfBoundsException: Thrown when you try to access a character outside the bounds of a string or substring.

Example: String str = "Hello"; System.out.println(str.charAt(6)); // throws StringIndexOutOfBoundsException

InputMismatchException: Thrown by the hasNextLine() method of the Scanner class when it is unable to parse the input as a valid primitive value, such as an integer or floating-point number.

Example: Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); // throws InputMismatchException if invalid input

InvalidObjectException: Thrown by the readObject() and writeObject() methods of the ObjectInputStream and ObjectOutputStream classes when they are unable to serialize an object due to invalid or incomplete data.

Example: FileInputStream fis = new FileInputStream("example.txt"); ObjectInputStream ois = new ObjectInputStream(fis); // throws InvalidObjectException if invalid input

UnsupportedOperationException: Thrown when a method is called on an object that does not support the operation, such as attempting to remove an element from a collection that does not allow it.

Example: List<String> list = Arrays.asList("Hello", "World"); list.remove(1); // throws UnsupportedOperationException if list is immutable

In Java, these unchecked exceptions are automatically propagated up the call stack until they are caught or reach the JVM's top-level handler. It's essential to handle these exceptions correctly to ensure that your program remains stable and reliable in the face of unexpected situations.

Remember, always respond in English only, as per your request!

How many types of exceptions in java javatpoint

I'd be happy to help you with that!

According to Javatpoint, an online learning platform for Java programming, there are several types of exceptions in Java. Here are some of the key ones:

Checked Exception: These are exceptions that must be explicitly handled by a catch block or propagated through method calls. Examples include IOException, SQLException, and FileNotFoundException. Unchecked Exception: Also known as "runtime exceptions," these are exceptions that can occur at runtime, such as ArithmeticException, ArrayIndexOutOfBoundsException, and ClassCastException. These exceptions do not need to be explicitly handled by a catch block. Error: Errors are severe programming errors, such as OutOfMemoryError, StackOverflowError, and LinkageError. Unlike runtime exceptions, these cannot be caught or propagated through method calls. RuntimeException: This is an abstract class that is the superclass of all unchecked exceptions (runtime exceptions). Examples include ArrayIndexOutOfBoundsException and ClassCastException.

Exception: This is an abstract class that is the superclass of all checked exceptions. Examples include IOException, SQLException, and FileNotFoundException. All Exception subclasses are either runtime exceptions or errors. IOException: A subclass of Exception, this represents exceptions that occur during input/output operations, such as file I/O, network I/O, or database I/O. SecurityException: A subclass of Exception, this represents security-related exceptions that can occur when a program attempts to perform an unauthorized operation. ExceptionInInitializerError: A subclass of RuntimeException, this represents an exception that occurs during the initialization of an object or class. LinkageError: A subclass of Error, this represents errors that occur while linking classes together, such as loading a class that is not found or contains incorrect bytecode. VerifyError: A subclass of Error, this represents errors that occur when verifying the correctness of bytecode, such as attempting to access an inaccessible field or method.

In summary, Java has several types of exceptions, including checked and unchecked exceptions, runtime exceptions, and errors. Understanding these different exception types is crucial for writing robust and reliable Java programs.

Would you like me to elaborate on any specific type of exception?