MCA
EXCEPTION HANDLING // ENTERPRISE CODE // ABSTRACT FACTORY // EXCEPTION HANDLING // ENTERPRISE CODE // ABSTRACT FACTORY //
BACK TO SYLLABUS
EASY

EXCEPTION HANDLING

Try-catch, custom exceptions, and error handling

CONCEPTS

01Checked vs Unchecked Exceptions
02try, catch, and finally blocks
03throw and throws keywords
04Creating custom exceptions
05Multi-catch blocks
06Try-with-resources

SYNTAX_DEMO

Writing resilient code
public class Main {
    public static double divide(int a, int b) throws Exception {
        if (b == 0) {
            throw new Exception("Cannot divide by zero");
        }
        return (double) a / b;
    }

    public static void main(String[] args) {
        try {
            System.out.println(divide(10, 0));
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            System.out.println("Execution completed.");
        }
    }
}