|11. Exception Handling
Chapter 11Java Tutorial~1 min read

Exception Handling

Exceptions — Errors Handle करणे

Exception म्हणजे program run होताना येणारी unexpected error. Exception handling वापरून program crash होण्यापासून वाचवतो. try-catch-finally — Java चा exception handling mechanism.

Marathi Analogy

Exception handling म्हणजे गाडीत spare tyre ठेवण्यासारखे. Puncture झाला (exception) तर घाबरायला नको — spare tyre (catch block) आधीच तयार ठेवला आहे. Program run होत राहतो!

try-catch

Basic exception handling

java
public class ExceptionDemo {
    public static void main(String[] args) {

        // Without handling — program crash होतो
        // int result = 10 / 0;  // ArithmeticException!

        // With try-catch
        try {
            int result = 10 / 0;   // Exception येतो येथे
            System.out.println(result);  // हे skip होते
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
            // Error: / by zero
        }

        System.out.println("Program चालू राहतो! ✅");
    }
}

Multiple Exceptions + finally

Multiple catch + finally

java
import java.util.Scanner;

public class MultipleExceptions {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        try {
            System.out.print("Number टाका: ");
            int num = sc.nextInt();         // InputMismatchException
            int[] arr = new int[5];
            arr[num] = 100;                  // ArrayIndexOutOfBoundsException
            System.out.println("Success: " + arr[num]);

        } catch (java.util.InputMismatchException e) {
            System.out.println("संख्या टाका, text नाही!");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("0-4 मधला number टाका!");
        } catch (Exception e) {
            // सगळ्या exceptions catch करतो — always last!
            System.out.println("Unexpected error: " + e.getMessage());
        } finally {
            // Exception आला असो वा नसो — हे नेहमी run होतो
            System.out.println("finally block — cleanup येथे");
            sc.close();
        }
    }
}

Common Exceptions

  • ArithmeticException — division by zero (10/0)
  • NullPointerException — null object वर method call
  • ArrayIndexOutOfBoundsException — invalid array index
  • NumberFormatException — "abc" ला Integer.parseInt() केले
  • ClassCastException — wrong type cast
  • StackOverflowError — infinite recursion

Custom Exception

Throw custom exception

java
// Custom exception class
class InvalidMarksException extends Exception {
    InvalidMarksException(String message) {
        super(message);
    }
}

// वापरणे
static void setMarks(int marks) throws InvalidMarksException {
    if (marks < 0 || marks > 100) {
        throw new InvalidMarksException(
            "Marks " + marks + " invalid! 0-100 असायला हवे."
        );
    }
    System.out.println("Marks set: " + marks);
}

// Main मध्ये
try {
    setMarks(85);   // OK
    setMarks(150);  // Exception throw होतो
} catch (InvalidMarksException e) {
    System.out.println("Error: " + e.getMessage());
}

Key Points — लक्षात ठेवा

  • try { risky code } catch (ExType e) { handle }
  • finally { } — नेहमी run होतो (cleanup, close resources)
  • throw — manually exception throw करतो
  • throws — method signature मध्ये declare
  • Custom exception: class MyEx extends Exception
0/11 chapters पूर्ण