Java Exceptions

  • There will be breakage while executing the code, eg if. tried divide by zero etc. such sort of condition need to be taken care in code. these conditions throws exception or error. This exceptions need to be handled in code for smooth flow.
  • The java.lang.Exception class is the root of the exception hierarchy in Java.
  • Throwable is the superclass for Exception and Error.
  • Exception is the superclass for checked & unchecked exception.

There are two main types of exceptions in Java: checked exceptions and unchecked exceptions.

  • Checked exceptions: These are exceptions that must be caught and handled by the code, otherwise, the code will not compile. Examples of checked exceptions include IOException, SQLException, and FileNotFoundException. These exceptions are generally checked exceptions.
  • Unchecked exceptions: These are exceptions that do not need to be caught or handled. Examples of unchecked exceptions include NullPointerException, IndexOutOfBoundsException, and IllegalArgumentException.

Java code for Checked & Unchecked Exception:

In this example, we have a Car class with a method setSpeed that sets the speed of the car. The method throws a SpeedOutOfRangeException if the specified speed is not within the allowed range of 0 to 150.

In the main method, we create a car object with speed 200, which is out of range and will result in a SpeedOutOfRangeException being thrown. We catch this exception and print an error message.

    Exception Handling: using try-catch

    To handle exceptions in the code, you need to wrap the code that might throw an exception within a try block and catch the exception in a catch block. The code within the try block is executed until an exception is thrown. If an exception is thrown, the control is transferred to the catch block that matches the type of exception thrown.

    Here’s an updated example of the code with exception handling:

    In this example, we have added a trycatch block around each instance of creating and setting the speed of a car. If an exception is thrown, it will be caught and the error message will be printed.

    Understanding try-catch-finally:

    The try-catch-finally combination in Java is used to handle exceptions that may occur in a program.

    Here’s how the try-catch-finally combination works:

    1. The code that might throw an exception is placed inside a try block.
    2. If an exception is thrown, the code inside the corresponding catch block will be executed. The catch block is used to handle the exception and can include code to recover from the exception or to log the error.
    3. The finally block is optional and contains code that will be executed regardless of whether an exception was thrown or not. It is typically used to release resources or to clean up after the try block has completed.

    Here’s an example of how the try-catch-finally combination can be used in a program:

    In this example, the code inside the try block tries to set the speed of the car to 200, which is outside the allowed range of 0 to 150. As a result, a SpeedOutOfRangeException is thrown and caught by the catch block, which prints an error message. Finally, the code inside the finally block is executed, which prints a message indicating that it has been executed.

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Scroll to Top