try-catch usage

O

Obnoxious User

Why does this:

try
{
throw 1;
}
catch(...)
{
handle_exception();
}

works as expected (exception is caught in the catch block)

and this:

try
{
throw;

I doubt this statement is even allowed here. It is used within
catch handlers to rethrow the exception so it can be catched
again somewhere.
 
V

vmnvmcxbv

Why does this:

try
{
throw 1;
}
catch(...)
{
handle_exception();
}

works as expected (exception is caught in the catch block)

and this:

try
{
throw;
}
catch(...)
{
handle_exception();
}

crashes application? Isn't catch(...) supposed to catch _everything_?

FWIW - I use VC++ 2008
 
N

Nick Keighley

Why does this:

try
{
    throw 1;}

catch(...)
{
    handle_exception();

}

works as expected (exception is caught in the catch block)

and this:

try
{
    throw;}

catch(...)
{
    handle_exception();

}

crashes application? Isn't catch(...) supposed to catch _everything_?

FWIW - I use VC++ 2008

you didn't throw _everything_ you threw nothing.
I believe the "naked" throw should only be used
for a rethrow of an exception that's beeen caught.
 
A

Ali Karaali

Why does this:

try
{
    throw 1;}

catch(...)
{
    handle_exception();

}

works as expected (exception is caught in the catch block)

and this:

try
{
    throw;}

catch(...)
{
    handle_exception();

}

The catch can catch everything but you don't throw anything.
So calling std::terminate() function
 
R

red floyd

Why does this:

try
{
    throw 1;}

catch(...)
{
    handle_exception();

}

works as expected (exception is caught in the catch block)

and this:

try
{
    throw;}

catch(...)
{
    handle_exception();

}

crashes application? Isn't catch(...) supposed to catch _everything_?

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

In other words, a "throw;" must occur inside a catch block, or you
will be terminated.
 
M

Michal

red floyd said:
Per 15.1/8, "If no exception is presently being handled, executing a /
throw-expression/ with no operand calls terminate()."

Hallo Red

I like Your answer very much!
Could YOu tell me where does this sentence go from?
best regards
Michal
 
E

Erik Wikström

Hallo Red

I like Your answer very much!
Could YOu tell me where does this sentence go from?

It's from the ISO C++ standard, section 15.1, the eighth paragraph.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top