Difference between throw and throws in java

tutorial and example
tutorial and example

This article shows you the core difference between “throw” and “throws”
keywords in Java programming language.
The throw keyword tells Java you want another part of the code to deal with
the exception, so it’s used to throw an exception explicitly, but throws is used
to declare exception in method signature.
You can declare one or more exception with throws like:

You can throw just one exception with throw like:

So difference between throw and throws is that:
Throw means an exception is actually being thrown, and throws
indicate that the method has the potential to throw that exception. The
following example uses both:

The first line declares that the method might or might not throw a
SQlException. Since this is a checked exception, the caller need to handle or
declare it.
The second line does throw an UnsupportedOperationException. Since this is
a runtime exception, it does not need to be declared on the first line and you
should catch this exception and handle it.

Another difference between throw and throws is that:
throws is followed by Exception class like: “IoException”.
But throw is followed by an instance of Exception class like:

Throw Example:

Throws Example:

As you read in previous examples, throws just declare exceptions that your method may or may not be thrown, if there is no error then your code run successfully without throwing any exceptions declared after throws keyword.
But when you write “throw new MyException(“Something error.”); ” then you explicitly throw new instance of the declared exception.

Very important thing is “Exception propagation”:

When method called “m1” called method “m2” which called “m3” then we call that is call stack. When something wrong occurred in “m3” it throws and exception, this exception will propagate from “m3” to “m2” to “m1” to … unless one of them catch this exception in catch clause.

Throwing a Second Exception

Most of us limit himself to one try statement in his code. However a catch or finally block can have any valid code in it including try statement.
Example: The following code tries to read a file: