Exception Handling Part 02

Exception Handling Part 02

Types Of Exception

  1. Checked Exception: In this type of exception, the exception will occur at the runtime, but only thing is that the compiler will tell us in advance that an exception may occur

  2. Unchecked Exception: In this type of exception, the exception will occur at runtime but the compiler will not inform us that an exception may occur

Exception Ducking

  1. Suppose we write a class and then we want to check for an exception, then we have to write multiple try-catch blocks in order to check and catch an exception

  2. Alternatively what we can do is we can let the caller try and catch the exception this is called an exception ducking

  3. The advantage is we the caller will check for exception

  4. We generally use this method for a checked exception

Re-throwing an Exception

  1. An exception can be rethrown in a catch block.

  2. This action will cause the exception to be passed to the calling method. If the rethrow operation occurs in the main method then the exception is passed to the JVM and displayed on the console

  3. The purpose of the rethrow operation is to get the attention of the outside world that an exception has occurred and at the same time perform any contingency logic (such as logging) in the catch block

public class RethrowException {
   public static void test1() throws Exception {
      System.out.println("The Exception in test1() method");
      throw new Exception("thrown from test1() method");
   }
   public static void test2() throws Throwable {
      try {
         test1();
      } catch(Exception e) {
         System.out.println("Inside test2() method");
         throw e;
      }
   }
   public static void main(String[] args) throws Throwable {
      try {
         test2();
      } catch(Exception e) {
         System.out.println("Caught in main");
      }
   }
}

Finally block

  1. Irrespective of what happens this block always be executed

    import java.io.*;
    
    class GFG {
        public static void main(String[] args)
        {
            try {
                System.out.println("inside try block");
    
                // Not throw any exception
                System.out.println(34 / 2);
            }
    
            // Not execute in this case
            catch (ArithmeticException e) {
    
                System.out.println("Arithmetic Exception");
    
            }
            // Always execute
            finally {
    
                System.out.println(
                    "finally : i execute always.");
    
            }
        }
    }
    Output
    
    inside try block
    17
    finally : i execute always.
    
    Case 2: When the exception rises and handled by the catch block
    
    In this case, the program throws an exception but handled by the catch block, and finally block executes after the catch block.
    
    // Java program to demonstrate finally block in java
    // When exception rise and handled by catch
    
    import java.io.*;
    
    class GFG {
    
        public static void main(String[] args)
        {
            try {
                System.out.println("inside try block");
    
                // Throw an Arithmetic exception
                System.out.println(34 / 0);
            }
            // catch an Arithmetic exception
            catch (ArithmeticException e) {
    
                System.out.println(
                    "catch : exception handled.");
            }
       // Always execute
            finally {  
    
              System.out.println("finally : i execute always.");
            }
        }
    }
    

    Finally vs return

    1. Always remember finally will dominate the return

    2. We cannot write finally alone always we have to use try block

example:

```java
class jeevan{
    public int disp(){
    try{    
        System.out.println("try catch");
        return 10;
    }
    finally{
        System.out.println("finally");
    }
}}





class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello, World!");


    jeevan object = new jeevan();

        System.out.println(object.disp());
     }
}

//here the output will be
Hello, World!
try catch
finally
10
```

## Finally along with System.exit()

```java
class jeevan{
    public void disp(){
    try{    
        System.out.println("try catch");
        System.exit(0);
    }
    finally{
        System.out.println("finally");
    }
}}





class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello, World!");



    jeevan object = new jeevan();

        object.disp();
     }
}

// output is
Hello, World!
try catch
```

## Rules For Overriding

1.  If the parent class throws unchecked exception then , the child class can throw any unchecked exception or the same exception but not any checked execption

Exception Class hierarchy

  1. Refer the link here Link

Throw vs Throws

  1. Throw is used to manually create the exception object

  2. Throws keyword will return the exception object to the caller

Checked & Unchecked Exception

  1. As we all know checked exception will occur at compile time, hence in order for the code to go ahead we need to handle it

  2. We can handle it using try catch or using throws keyword

  3. Unchecked exceptions occur at compile time hence there is no need for us to check it