Exceptions: Throws Keyword Tutorial

The throws keyword is used to create a list of checked exceptions that are not handled in the code block of either a method or a constructor. A method or constructor does not need to specify any unhandled unchecked exceptions in the throws list.
The throws list must be after the method or constructor signature and before the opening curly brace of the code block:
( return type ) ( methodName(parameter list) )( throws ) ( ExceptionClass ) { method body }

Java exception handling has a principle called the catch or specify requirement that applies to checked exceptions. A method or constructor body that invokes a method or a constructor that throws a checked exception must either handle that exception with a try/catch statement, or specify that checked exception in the throws list.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ExceptionThrows
C:\Java>cd ExceptionThrows
C:\Java\ExceptionThrows>Notepad ExceptionThrows.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;
class ExceptionThrows {
    public static void main(String args[]) {
        ThrowSomeStuff tss = new ThrowSomeStuff();
    
        try {
            tss.throwAnIOE();
        } catch ( IOException e) {
            System.out.println("Caught the IOException: " + e);
        }

        try {
            tss.makeBoxError();
        } catch (Exception e ) {
            System.out.println("Caught the box exception: " + e.getMessage());
        }
    }
}

class ThrowSomeStuff {   
    void throwAnIOE() throws IOException {
        throw new IOException();
    }
    void makeBoxError() throws InvalidMeasurementException {
        Box b = new Box( 9, -5, 4);
    }
}

class Box {
    private int length = 0;
    private int height = 0;
    private int width = 0;

    Box(int length, int height, int width) throws InvalidMeasurementException {
        if (length <= 0 || height <= 0 || width <= 0) {
            throw new InvalidMeasurementException();
        }
        this.length = length;
        this.height = height;
        this.width = width;
    }

    int getVolume() {
        return (length * height * width);
    }
}

class InvalidMeasurementException extends Exception {
    InvalidMeasurementException() {
        super("Measurement argument cannot be 0 or negative");
    }
}

Now switch back to the command prompt (CMD) and type in javac ExceptionThrows.java and press Enter.
Now type in java ExceptionThrows and press Enter.


C:\Java\ExceptionThrows>javac ExceptionThrows.java
C:\Java\ExceptionThrows>java ExceptionThrows
Caught the IOException: java.io.IOException
Caught the box exception: Measuerment argument cannot be 0 or negative


Final thoughts

A method or constructor body that invokes a method or a constructor that throws a checked exception must either handle that exception with a try/catch statement, or specify that checked exception in the throws list.


Tutorials