थ्रोइंग

Java में डेटा टाइप्स और Exceptions

Jim White

Java Developer

Handling options

  • Checked exceptions के लिए "handling" ज़रूरी है
    • Runtime exceptions का "handling" वैकल्पिक है
  • "Handling" के दो विकल्प
    • Exception कैप्चर करने के लिए try और catch का उपयोग करें और समस्या सुलझाने का कोड लिखें
    • Exception को "throw" करें
  • Exception संभालने के लिए try/catch पसंदीदा तरीका है
    • हमेशा संभव नहीं, और कभी-कभी हैंडलिंग को समेकित करना बेहतर होता है
Java में डेटा टाइप्स और Exceptions

Throws keyword

  • Use throws on any method to pass the exception back to the calling method
    • Versus handling the exception in the method
    • Called "throwing an exception" or "passing-the-buck"
    • Passing the responsibility for handling the exception to a caller
  • Use a comma separated list of exception types after throws
    • Indicates what types of exceptions the caller method might anticipate
public static void someMethod() throws IndexOutOfBoundsException, 
NegativeArraySizeException, NullPointerException {
  // method code
}
Java में डेटा टाइप्स और Exceptions
Throws example

Normal try-catch to handle an Exception

public static void main(String[] args) {
  someMethod();
}
public static void someMethod(){
  try {
    ArrayList<String> games
      = new ArrayList<String>();
    games.add("Monopoly");
    games.add("Chess");
    games.get(3);
  } catch (IndexOutOfBoundsException e) {
    System.out.println(
   "Oops - trying to get non-existent item");
  }
}

Throws to handle an Exception

public static void main(String[] args) {
  try {
    someMethod();
  } catch (IndexOutOfBoundsException e) {
    System.out.println(
   "someMethod tried to get non-existent item");
  }
}
public static void someMethod()
      throws IndexOutOfBoundsException {
  ArrayList<String> games
    = new ArrayList<String>();
  games.add("Monopoly");
  games.add("Chess");
  games.get(3);
}
Java में डेटा टाइप्स और Exceptions

When to use throws

  • "Passing-the-buck" or throwing an exception is something we try to avoid
    • Better practice is, usually, to try-catch exceptions where they happen
  • Reasons for throwing rather than using try-catch include:
    • The method where the exception occurs may not know how to recover
    • Centralizing the handling to reduce redundant try/catch handling code

Throwing an exception is typically used with custom exceptions

1 Photo from https://unsplash.com/@philipparosetite
Java में डेटा टाइप्स और Exceptions

Catch and rethrow

  • An Exception can be "rethrown"
    • A calling method also calls throws instead of having a try/catch
  • Rethrowing from main will cause the application to stop

Exceptions can be rethrown to calling methods

Java में डेटा टाइप्स और Exceptions

Rethrowing example

public class RethrowExample {
    public static void main(String[] args) {
        try {
            method1(0);
        } catch (ArithmeticException e) {
            System.out.println("Oops - tried a bad quotient");
        }
    }
    public static void method1(int divisor) throws ArithmeticException {
        method2(divisor);
    }
    public static void method2 (int divisor) throws ArithmeticException {
        int z = 5/divisor;
        System.out.println(z);
    }
}
Java में डेटा टाइप्स और Exceptions

Let's practice!

Java में डेटा टाइप्स और Exceptions

Preparing Video For Download...