Skip to content

Instantly share code, notes, and snippets.

@Visnusah
Created January 1, 2025 07:23
Show Gist options
  • Save Visnusah/5c5835466521d1e9ae4e3d2680f86695 to your computer and use it in GitHub Desktop.
Save Visnusah/5c5835466521d1e9ae4e3d2680f86695 to your computer and use it in GitHub Desktop.
TeamWarica
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
@Visnusah
Copy link
Author

Visnusah commented Jan 5, 2025

Exception Handling In Java

public class ExceptionHandling{
    public static void main(String[] args) {
        System.out.println("Start of program");

        // Attempting to divide by zero to demonstrate ArithmeticException
        try {
            int num1 = 100/0; // This line will throw ArithmeticException
            System.out.println("Value of num1: "+num1);
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught: " + e);
        }

        // try - catch - finally block to handle ArithmeticException
        try{
            int num2 = 100/0; // This line will throw ArithmeticException
            // If an exception occurs, the program will jump to the catch block
            // If an exception occurs, the following line will not be executed
            System.out.println("Value of num2: "+num2);
        }catch(ArithmeticException ex){
            // Catching ArithmeticException and printing the exception message
            System.out.println("ArithmeticException caught: " + ex);
        }

        // Array exception handling
        try
        {
            int[] numbers = {1, 2, 3, 4, 5};
            System.out.println(numbers[10]); // This line will throw ArrayIndexOutOfBoundsException
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            // Catching ArrayIndexOutOfBoundsException and printing the exception message
            System.out.println("ArrayIndexOutOfBoundsException caught: " + ex);
        }

        // NULL pointer exception handling
        String name = null; // Setting a string to null to demonstrate NullPointerException
        try {
            {
                System.out.println(name.length()); // This line will throw NullPointerException
            }
        } catch (NullPointerException e) { 
            // Catching NullPointerException and printing the exception message
            System.out.println("NullPointerException caught: " + e);
        }

        // common Catch block to catch any type of exception
        try
        {
            int num2 = 100/0; // This line will throw ArithmeticException
        }
        catch (Exception e)
        { 
            // Catching any type of exception and printing the exception message
            System.out.println("Common Exception caught: " + e);
        }
        finally
        {
            // This block will always execute, regardless of whether an exception occurred or not
            System.out.println("Finally block executed");
        }

        // Multiple catch blocks to handle different types of exceptions
        // Attempting to access an array index that is out of bounds to demonstrate exception handling
        try{
            int[] numbers2 = {1, 2, 3, 4, 5};
            System.out.println(numbers2[10]); // This line will throw ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException error) {
            // Catching ArrayIndexOutOfBoundsException and printing the exception message
            System.out.println("ArrayIndexOutOfBoundsException caught: " + error);
        } catch(ArithmeticException error) {
            // Catching ArithmeticException and printing the exception message
            System.out.println("ArithmeticException caught: " + error);
        } catch (Exception error) {
            // Catching any other type of exception and printing the exception message
            System.out.println("Common Exception caught: " + error);
        }

        
        System.out.println("End of program");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment