Exception Handling in Java | What are the exceptions in Java?

 






Exception handling in Java is a mechanism that allows the application to handle unexpected and exceptional conditions in a systematic way. The goal of exception handling is to handle these exceptional conditions in a controlled and predictable manner, allowing the application to continue its normal execution.


  • An exception is a runtime error that occurs during the execution of a program. Java provides a way to handle exceptions using a try-catch-finally block. 
  • The try block contains the code that may throw an exception. 
  • The catch block is used to catch the exception and handle it in a meaningful way.
  •  The finally block is used to execute code that must be executed, regardless of whether an exception occurs or not.


The hierarchy of exceptions in Java is organized around the Throwable class, which is the root of the exception class hierarchy in Java. 

The Throwable class has two direct subclasses: Error and Exception.


Error: This class represents severe errors that are typically beyond the control of the application, such as out-of-memory errors or stack overflow errors. These types of errors usually indicate a problem with the system or the JVM, and are not recoverable.


Exception: This class represents exceptions that are typically caused by problems within the application, such as incorrect data or invalid operations. These types of exceptions can be caught and handled by the application, allowing the program to continue its normal execution.



Exception class has two direct subclasses: 

  • RuntimeException
  • CheckedException


RuntimeException: This class represents runtime exceptions, which are exceptions that are thrown at runtime and do not need to be declared in a throws clause. Examples of runtime exceptions include NullPointerException and IllegalArgumentException.


CheckedException: This class represents checked exceptions, which are exceptions that must be declared in a throws clause. Examples of checked exceptions include FileNotFoundException and SQLException.


Java provides several built-in exception classes that represent common types of exceptions that can occur in a Java program. These exceptions are used to indicate different kinds of errors or exceptional conditions. 



Some of the most commonly used exception classes in Java are:


ArithmeticException: This exception is thrown when an arithmetic operation such as division by zero occurs.


NullPointerException: This exception is thrown when a null object is used in a method that requires a non-null object.


IndexOutOfBoundsException: This exception is thrown when an index is used to access an array or a list that is outside its valid range.


NumberFormatException: This exception is thrown when a string cannot be parsed as a number.


ClassCastException: This exception is thrown when an object cannot be cast to a specific type.


IllegalArgumentException: This exception is thrown when a method is passed an illegal or inappropriate argument.


FileNotFoundException: This exception is thrown when a file cannot be found on the file system.


IOException: This exception is thrown when an input/output error occurs.


SQLException: This exception is thrown when a database operation fails.


NoSuchMethodException: This exception is thrown when a method cannot be found in a class.


It is important to note that these exception classes are part of the Java standard library and are used to indicate different types of errors that can occur in a Java program. When handling exceptions, it is a good practice to catch only the specific exceptions that you expect to occur in your code and to provide meaningful error messages to the user.



Here's an example of how exception handling works in Java:



try {
 
   int result = divide(10, 0);
 
   System.out.println("The result is: " + result);
 
} catch (ArithmeticException e) {
 
   System.out.println("An arithmetic exception occurred: " + e.getMessage());
 
} finally {
 
   System.out.println("This code is always executed, even if an exception occurs.");
 
}


In this example, the divide method may throw an ArithmeticException if the denominator is zero. The try block contains the code that calls the divide method. The catch block is used to catch the ArithmeticException and handle it by printing a meaningful error message. The finally block contains code that is always executed, regardless of whether an exception occurs or not.


Java provides several built-in exception classes, such as ArithmeticException, NullPointerException, and IndexOutOfBoundsException, to represent common types of exceptions. You can also define your own custom exceptions by extending the Exception class.


Summary: Exception handling in Java is an important aspect of Java programming that allows developers to handle unexpected and exceptional conditions in a controlled and predictable manner, leading to robust and reliable applications.


Post a Comment

Post a Comment (0)

Previous Post Next Post