Throwing Exceptions - Java Language Basics

Java examples for Language Basics:throw

Introduction

Use the throw keyword to throw a specified exception.

Demo Code

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

    try {/*  ww  w.  j  a va2  s  .c om*/
      callSomeFunctionThatMightThrow(null);
    } catch (NullPointerException e) {
      System.out.println("There was an null parameter!");
    }

  }

  private static void callSomeFunctionThatMightThrow(Object o) {
    if (o == null)
      throw new NullPointerException("The object is null");

  }
}

Result


Related Tutorials