Throws keyword in java exception

If you do not want to handle the exception and let the caller of method handle it, you can achieve this by using throws keyword in java.

So the caller has to either catch or throws it again.

For example we have doStuff() method which throws the type Exception, so whenever any caller call this method. It has to either put it inside try block or throws it to next caller.


public void doStuff() throws Exception {
// write some statements checked or unchecked
}

The throws keyword appears at the end of a method’s signature.