Throwing

Data Types and Exceptions in Java

Jim White

Java Developer

Handling options

  • Checked exceptions require "handling"
    • "Handling" runtime exceptions is optional
  • Two options for "handling"
    • Use a try and catch to capture the Exception and write code to address the issue
    • "Throw" the exception
  • Try/catch is preferred means to handle an exception
    • Not always possible and sometimes better to consolidate exception handling
Data Types and Exceptions in Java

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
}
Data Types and Exceptions in Java
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);
}
Data Types and Exceptions in Java

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
Data Types and Exceptions in Java

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

Data Types and Exceptions in Java

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);
    }
}
Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...