Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Java Exception Handling

Tech 1

In Java, exception handling is a mechanism for handling errors or exceptional situations that ocurr during program execution. Java provides try-catch blocks to catch and handle exceptions, and finally blocks to perform cleanup operations. Below is a simple introduction and example code for Java exception handling.

Basic Structure of Exception Handling

  • try block: Contains code that may throw a exception.
  • catch block: Catches and hendles the exception thrown in the try block.
  • finally block: Code block that executes regardless of whether an exception occurs.

Example Code

public class ExceptionHandlingDemo {
    public static void main(String[] args) {
        try {
            // Code that may throw an exception
            int result = divide(10, 0);
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            // Catch and handle ArithmeticException
            System.out.println("Caught ArithmeticException: " + e.getMessage());
        } finally {
            // Code that always executes
            System.out.println("Finally block executed");
        }
    }

    public static int divide(int a, int b) {
        return a / b; // This may throw ArithmeticException
    }
}

Code Explanation

  • try block:

    try {
        int result = divide(10, 0);
        System.out.println("Result: " + result);
    }
    

    This calls the divide method and attempts to print the result. Since the divisor is 0, an ArithmeticException is thrown.

  • catch block:

    catch (ArithmeticException e) {
        System.out.println("Caught ArithmeticException: " + e.getMessage());
    }
    

    Catches the ArithmeticException and prints the error message.

  • finally block:

    finally {
        System.out.println("Finally block executed");
    }
    

    Regardless of whether an exception occurs, the code in the finally block executes.

Output

Caught ArithmeticException: / by zero
Finally block executed

In this way, Java programs can gracefully handle runtime exceptions, avoid crashes, and provide useful error information.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.