Difference between throw and throws in java
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:
public Date getDate() throws RuntimeException, DateTimeParseException.
You can throw just one exception with throw like:
throw new RuntimeException();
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:
public User getUserData(int id) throws SQLException{
throw new UnsupportedOperationException();
}
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 new IOException("IOException is fired.");
Throw Example:
public void writeInFile () {
try {
throw new IOException("File not found");
} catch(Exception e ){
System.out.println(“The error is: ” + e.getMessage());
}
}
Throws Example:
public void writeInFile () throws IoException, RuntimeException, NullPointerException{
try {
// Error occurred here cause by Null pointer exception
} catch(Exception e ){
System.out.println(“The error is: ” + e.getMessage());
throw new NullPointerException();
}
}
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:
private FileReader readFile() throws IOException{
// your code here.
}
private testReadFile () {
FileReader fReader = null;
try {
fReader = readFile()
} catch (IOException e ) {
try {
if (fReader != null){
fReader.close();
}
} catch (Exception e ){
// do something
}
}
}