An exception is an event that disrupts the normal flow of a program's instructions during execution. Exceptions can occur for various reasons, such as invalid input, resource unavailability, or programming errors.
Exception handling is a fundamental feature that allows developers to manage and respond to exceptional conditions or errors that occur during the execution of a Java program. Java provides a robust and structured mechanism for handling exceptions.
- Hierarchy of Java Exception classes
- Types of Java Exceptions
There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:
- Checked Exception
- Unchecked Exception
- Error
Checked Exceptions:- Checked exceptions are exceptions that are checked by the compiler at compile time. This means that the compiler ensures that these exceptions are either caught or declared to be thrown by the method where they can occur. Examples of checked exceptions include
IOException
, FileNotFoundException
, ParseException
, and SQLException
.
Unchecked Exceptions (Runtime Exceptions):
- Unchecked exceptions, also known as runtime exceptions, are exceptions that are not checked by the compiler at compile time. Instead, they occur at runtime and are not required to be caught or declared by the method where they occur.
- Unchecked exceptions usually represent programming errors or logical errors in the code. They can occur due to incorrect use of APIs, improper data manipulation, or invalid arguments to methods. Examples of unchecked exceptions include
NullPointerException
, ArrayIndexOutOfBoundsException
, ArithmeticException
, and IllegalArgumentException
.
Error:
- Errors in Java represent serious problems that are typically beyond the control of the application. They usually indicate issues with the runtime environment or system resources.
- Examples of errors include
OutOfMemoryError
, StackOverflowError
, and VirtualMachineError
.
Java try block is used to enclose the code that might throw an exception. It must be used within the method.
If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.
Java try block must be followed by either catch or finally block.
Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type
Java finally block is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not.
public class ExceptionHandlingExample { public static void main(String[] args) { try { int data = 50 / 0; // may throw exception System.out.println("Result: " + data); } catch (ArithmeticException e) { System.err.println("Error: Division by zero is not allowed."); } finally { System.out.println("Cleanup code executed."); } }}
A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-catch block.
public class MultipleCatchBlock1 { public static void main(String[] args) { try { int a[] = new int[5]; a[5] = 30/0; } catch(ArithmeticException e) { System.out.println("Arithmetic Exception occurs"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBounds Exception occurs"); } catch(Exception e) { System.out.println("Parent Exception occurs"); } System.out.println("rest of the code"); } }
public class MultipleExceptionExample { public static void main(String args[]) { try { int array[] = new int[10]; array[10] = 30/0; } catch (Exception | ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } }
There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely:
- Checked Exception
- Unchecked Exception
- Error
- Checked exceptions are exceptions that are checked by the compiler at compile time. This means that the compiler ensures that these exceptions are either caught or declared to be thrown by the method where they can occur. Examples of checked exceptions include
IOException
,FileNotFoundException
,ParseException
, andSQLException
.
Unchecked Exceptions (Runtime Exceptions):
- Unchecked exceptions, also known as runtime exceptions, are exceptions that are not checked by the compiler at compile time. Instead, they occur at runtime and are not required to be caught or declared by the method where they occur.
- Unchecked exceptions usually represent programming errors or logical errors in the code. They can occur due to incorrect use of APIs, improper data manipulation, or invalid arguments to methods. Examples of unchecked exceptions include
NullPointerException
,ArrayIndexOutOfBoundsException
,ArithmeticException
, andIllegalArgumentException
.
Error:
- Errors in Java represent serious problems that are typically beyond the control of the application. They usually indicate issues with the runtime environment or system resources.
- Examples of errors include
OutOfMemoryError
,StackOverflowError
, andVirtualMachineError
.
Java try block is used to enclose the code that might throw an exception. It must be used within the method.
If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.
Java try block must be followed by either catch or finally block.
Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type
The Java throw keyword is used to throw an exception explicitly. We can throw either checked or unchecked exceptions in Java by throw keyword.
Throwing an unchecked exception doesn't require you to declare it using the throws
clause in the method signature or handle it with a try-catch
block.
For checked exceptions, it is indeed required to either handle the exception using a
try-catch
block or declare the exception in the method signature using the throws
keyword.If we are calling a method that declares an exception, we must either caught or declare the exception.
There are two cases:
- Case 1: We have caught the exception i.e. we have handled the exception using try/catch block.
- Case 2: We have declared the exception i.e. specified throws keyword with the method.
There are many rules if we talk about method overriding with exception handling.
Комментариев нет:
Отправить комментарий