If you are not handling a particular type of exception then you can throw it again so that some other functions can handle it. The syntax of re-throw is just a throw statement where you will not be able to specify any object.
throw;
If you want to throw a different type of exception, then catch the first exception and then throw your new object with a regular throw statement.
my addendum to athachil's good answer:
1. make sure what does it mean by "rethrow" in your interview question.
2. if "rethrow" means "throw;", then you have no choice but to throw the "same" object again. even this exception object may be modified in this level of catch, but "throw;" will not record any modification and throw only the original object which is catched by this level.
3. see athachil's last example.
ok... Now.. I thought about this... lets say we have the following scenario
1 2 3 4 5 6 7 8 9 10 11 12
function Somefunction()
{
try{
// some code that throws an exception
aFunctionThatThrowsException();
}
catch(Exception& e)
{
throw MyNewException();
}
}
So in this case, you might have to the exception thrown by the code in someFunction diferently than that thrown by the aFunctionThatThrowsException()..
So in such case if put throw MyNewException(); in the catch block then the new exception would be thrown rite and the function that invokes Somefunction() would always see the myNewException irrespective of who throws the exception...
@moorecm..
I am not talking abt a single exception transforming into a new one..!! but what i mean is, who do we deal with exceptions thrown by two different parts of the code..
for example.. this scenario is easy..
1 2 3 4 5 6 7 8 9 10 11 12 13
function Somefunction()
{
try{
// some code that throws an exception
aFunctionThatThrowsException();
oneMoreFunctionThatThrowsException();
}
catch(Exception& e)
{
throw;
}
}
In the above case we just throw the exception.. So you are just passing on the exception you get.. So no problem.. The problem is in the above case where I want to deal the exception that occurs in the local code and pass on the exception that is caught from aFunctionThatThrowsException();
In that scenario, two things come to mind. First, you could use different try and catch blocks for your local code than for the function calls. Second, you do not have to catch all exceptions so that some of them propagate to another catch.
The above code causes System.DivideByZeroException (I use Visual Studio 2010) how to we catch that?
Are you sure it throws that? Try catch(System::DivideByZeroException & e) {/*...*/} and see if it works. If it does, then you're right.
In general, there are two kinds of exceptions, hardware and software ones (much like assembly interrupts). Hardware exceptions are raised by the CPU, while software exceptions are raised by applications and the OS. The easy thing you can do with your compiler is catch the software exceptions raised by your application. If you want to catch OS raised exceptions or even hardware exceptions you'll have to struggle a bit more...