throw; rethrows the same exception object it caught while throw ex; throws a new exception. It does not make a difference other than the performance reasons of creating a new exception object. If you have a exception hierarchy where there some other exception classes derived from MyException class and while throwing an exception you have done a throw DerivedClassException; it can be caught by the catch(MyException&). Now if you modify this caught exception object and rethrow it using throw; the type of exception object will still be DerivedClassException. If you do throw Ex; the object slicing occurs and the newly thrown exception will be of type MyException.
Videos
What is the difference between throw and throws in Java?
What is the syntax for throw and throws in Java?
Can both throw and throws in Java declare both unchecked and checked exceptions?
throw; rethrows the same exception object it caught while throw ex; throws a new exception. It does not make a difference other than the performance reasons of creating a new exception object. If you have a exception hierarchy where there some other exception classes derived from MyException class and while throwing an exception you have done a throw DerivedClassException; it can be caught by the catch(MyException&). Now if you modify this caught exception object and rethrow it using throw; the type of exception object will still be DerivedClassException. If you do throw Ex; the object slicing occurs and the newly thrown exception will be of type MyException.
If you have an exception hierarchy, throw ex can slice your exception, while throw won't. For example:
#include <iostream>
#include <string>
using namespace std;
struct base
{
virtual string who() {return "base";}
};
struct derived : public base
{
string who() {return "derived";}
};
int main() {
try {
try {
throw derived(); // throws a 'derived'
}
catch (base& ex)
{
throw ex; // slices 'derived' object to be a 'base' object
}
}
catch (base& ex)
{
cout<<ex.who()<<endl; // prints 'base'
}
}
Change throw ex to just throw, and you'll get an output of derived, which is what you probably expected to get.
throwsclause is used to declare an exception andthrowkeyword is used to throw an exception explicitly.If we see syntax wise then
throwis followed by an instance variable andthrowsis followed by exception class names.The keyword
throwis used inside method body to invoke an exception andthrowsclause is used in method declaration (signature).
For example
throw
throw new Exception("You have some exception")
throw new IOException("Connection failed!!")
throws
public int myMethod() throws IOException, ArithmeticException, NullPointerException {}
You cannot declare multiple exceptions with
throw. You can declare multiple exception e.g. public void method()throws IOException,SQLException.checked exceptions can not be propagated with
throwonly because it is explicitly used to throw an particular exception. checked exception can be propagated withthrows.
Exception propagation: An exception propagates from method to method, up the call stack, until it's caught. So if a() calls b(), which calls c(), which calls d(), and if d() throws an exception, the exception will propagate from d to c to b to a, unless one of these methods catches the exception. what is exception propagation?
throw use for throwing actual Exception and throws declare at method it might throws Exception.
public int findMax(int[] array) throws Exception{
if(array==null)
throw new NullPointerException(...);
...
}