Java Language Tutorial:

Java Language Exceptions and exception handling

Introduction

Objects of type Throwable and its subtypes can be sent up the stack with the throw keyword and caught with try…catch statements.

Syntax

  • void someMethod() throws SomeException { } //method declaration, forces method callers to catch if SomeException is a checked exception type

  • try {

    someMethod(); //code that might throw an exception 
    

    }

  • catch (SomeException e) {

     System.out.println("SomeException was thrown!"); //code that will run if certain exception (SomeException) is thrown
    

    }

  • finally {

     //code that will always run, whether try block finishes or not
    

    }

Java Language Exceptions and exception handling Examples:

Related Java Language Tutorial Topics:

Other Tutorial Topics:

This modified content is an extract of the original Stack Overflow Documentation released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow. This topic was created and modified by the Stack Ovedrflow Contributor.