Java Exception Indepth Research

The exception mechanism is how the program is handled when the program meets an error. It provides a safe exit method for the java programs. When an error occurs, the program executing process changes, and the control of the program is transferred to the exception handler.

1. Exception Handling Process.

  1. When the program throws an exception, the program will jump out of the code that causes it.
  2. Java virtual machine will find the related catch block which can handle it. If found, execute the java code in the catch block and exit the java code in the try block.
  3. If not found, then after the java code in “finally block” execute, the program will throw it out to it’s parent class.

2. Class Structure.

  1. From the below picture we can see that Throwable is the base class, Error and Exception inherit Throwable,  RuntimeException and IOException inherit Exception. We will explain each of them in the below list.

2.1 Error.

  1. The Error indicates that the program meets a very serious, unrecoverable error at run time.
  2. In such case, the application can only stop running.
  3. Error is an UncheckedException, the compiler will not check whether the error is handled or not in the program.
  4. You do not have to capture the error also.
  5. Generally, you do not need to throw an Error in your java code.

2.2 RuntimeException.

  1. Java Exception includes RuntimeException and others that are not RuntimeException.
  2. It is an UncheckedException.
  3. Java compiler will not check whether the java code process RuntimeException or not.
  4. You do not need to catch it in java code and do not need to declare it in the method declaration.
  5. When a RuntimeException occurs, it indicates that a programming error has occurred in the program, so you should find the error and fix it instead of catching the RuntimeException.

2.3 CheckedException.

  1. This is also the most used in java programming. They all inherited from Exception class except RuntimeException. Such as IOException and ClassNotFoundException in the above picture.
  2. JAVA language specifies that the CheckedException must be processed, the compiler will check it.
  3. You can either declare them in the java method declaration or use the catch statement to catch it to process, otherwise, it will not pass the compiler’s check.

2.4 Throws an exception when declaring a method.

  1. Grammar: throws.

2.5 Why should we throw it at method declaration?

  1. Whether the method throws an exception or not is as important as the type of return value of the method.
  2. Assuming that a method throws an exception but does not declare it, then when java code calls this method, it will not have to write code to handle it also.
  3. So, once occurred, there is not any catch block that can control it.

2.6 Why thrown CheckedException?

  1. RuntimeException and Error can be generated in any code, they do not need to be thrown by the java programmer.
  2. When it occurred, a corresponding error will be thrown automatically.
  3. When your java code encounter Error or RuntimeException, there must have a logical error in java code.
  4. Only the CheckedException is what a programmer should concern with. The program should and should only throw or process it.

2.7 How to throw in method body?

  1. Grammar: throw.
  2. For an exception object, the really useful information is its class type. For example, if it’s class type is ClassCastException, then the class is more useful than the object.
  3. So when you choose which exception to catch, the most critical is to select the class that can clearly explain the abnormal situation.
  4. Exception objects usually have two constructors: One is a parameter-less constructor, the other is a constructor with a string input parameter, the string parameter provides the extra explanation to the caller.

2.8 Why do you need to create your own?

  1. When Java built-in exception can not clearly explain the abnormal situation, you need to create your own.

2.9 The difference between throw and throws.

  1. From the below java code example, you can see when and how to use the keyword throw and throws in the java method.
    public class TestThrowException {
    public static void main(String[] args) {
        /* Call the method with the throws declaration, 
        * you must explicitly catch the exception it thrown.
        * Otherwise, you must throw it again in the main method. */
        }catch(Exception ex) 
           System.out.println("CheckedException catched.");
        /* Call the method that throws the RuntimeException.
        * You can either explicitly capture or ignore it. */
        System.out.println("Program finished.");
    /* Throws at method declaration. */
    public static void throwCheckedException(boolean checkedException) throws Exception 
           /* Throw CheckedException. */
           throw new Exception("This is a thrown CheckedException. "); 
    /* RuntimeException do not need to be thrown in method declaration. */
    public static void throwRuntimeException(boolean checkedException) 
        if (!checkedException) 
           /* Throw RuntimeException. */
           throw new RuntimeException("This is a RuntimeException. "); 
  2. Added: Another style of the throwCheckedException method is as following:
     public static void throwCheckedException(boolean checkedException)
              /* Throw CheckedException. */
              throw new Exception("This is a thrown CheckedException. "); 
           }catch(Exception ex)
  3. Note: Now do not need try catch block in the main method because the method throwCheckedException catches the thrown exception inside.

2.10 Should you throw it at the method declaration or catch it in the method body?

  1. Handling principles: catch and handle exceptions that you know how to handle, and throw those ones that you do not know.

2.11 Use the finally block to release the resource.

  1. The finally keyword ensures that the java code in the finally block will be executed regardless of how the program leaves the try block.
  2. The finally block will be entered in the following three cases:
  3. The try block java code completes normally.
  4. Try block java code throws an exception.
  5. Execute return, break, continue in the try block.
  6. So, when you need a place to execute the code that must be executed in any case, you can put the code in the finally block.
  7. When your program uses external resources, such as database connections, files, etc. You must write the code that releases these resources in the finally block.
  8. It is important to note that you had better do not throw exceptions in the finally block.
  9. The JAVA code handling mechanism ensures that the code in the finally block should be executed first and then leave the try block.
  10. So when an exception occurs in the try block, the JAVA virtual machine first goes to the finally block to execute the code in it. After that, the exception object will be thrown out.
  11. If the exception is thrown in the finally block, then the exception that occurred in the try block can not be thrown. The exception that is caught by the external java code will show the error message in the finally block, and the real error information in the try block will be lost.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.