Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I use "throw;" without an exception to throw?

Here's the setup.

I have a C++ program which calls several functions, all of which potentially throw the same exception set, and I want the same behaviour for the exceptions in each function (e.g. print error message & reset all the data to the default for exceptionA; simply print for exceptionB; shut-down cleanly for all other exceptions).

It seems like I should be able to set the catch behaviour to call a private function which simply rethrows the error, and performs the catches, like so:

void aFunction()
{
    try{ /* do some stuff that might throw */ }
    catch(...){handle();}
}

void bFunction()
{
    try{ /* do some stuff that might throw */ }
    catch(...){handle();}
}

void handle()
{
    try{throw;}
    catch(anException)
    {
        // common code for both aFunction and bFunction
        // involving the exception they threw
    }
    catch(anotherException)
    {
        // common code for both aFunction and bFunction
        // involving the exception they threw
    }
    catch(...)
    {
        // common code for both aFunction and bFunction
        // involving the exception they threw
    }
}

Now, what happens if "handle" is called outside of the exception class. I'm aware that this should never happen, but I'm wondering if the behaviour is undefined by the C++ standard.

like image 699
deworde Avatar asked Feb 18 '10 12:02

deworde


People also ask

Can I use throw without throws?

Yes, we can throw an exception manually using throw keyword without throws.

What happens if a thrown exception is not caught?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

How can I catch exception without throwing exception?

Without using throws When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). If you re-throw the exception, just like in the case of throws clause this exception now, will be generated at in the method that calls the current one.

Can you throw without try?

Yes it is Ok to throw an exception when it isn't inside a try block. All you have do is declare that your method throws an exception. Otherwise compiler will give an error.


2 Answers

If handle() is called outside the context of an exception, you will throw without an exception being handled. In this case, the standard (see section 15.5.1) specifies that

If no exception is presently being handled, executing a throw-expression with no operand calls terminate().

so your application will terminate. That's probably not what you want here.

like image 107
John Feminella Avatar answered Oct 26 '22 22:10

John Feminella


If you use throw inside of a catch block, it will rethrow the exception. If you use throw outside of a catch block, it will terminate the application.

like image 33
Brian R. Bondy Avatar answered Oct 26 '22 21:10

Brian R. Bondy