473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

try-catch usage

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
Aug 26 '08 #1
5 1408
On Tue, 26 Aug 2008 12:01:20 +0000, vmnvmcxbv wrote:
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.

--
OU
Aug 26 '08 #2
On 26 Ađustos, 15:01, "vmnvmcxbv" <ruyeir...@fhsdkhf.netwrote:
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

Aug 26 '08 #3
On Aug 26, 5:01*am, "vmnvmcxbv" <ruyeir...@fhsdkhf.netwrote:
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.
Aug 26 '08 #4
red floyd wrote:
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.
I think that's too narrow. An exception can be handled even if you are
not currently inside of a catch block, can't it?

#include <exception>
#include <stdexcept>
#include <iostream>

void handleException()
{
try
{
throw;
}
catch (std::exception const &exc)
{
std::cout << exc.what() << "\n";
}
catch (...)
{
std::cout << "unknown exception\n";
}
}

int main()
{
try
{
throw std::runtime_error("test");
}
catch (...)
{
handleException();
}
}

Isn't this a perfectly standard-conforming program? It does not call
terminate() when compiled and run with VC or GCC.
--
Christian Hackl
Aug 26 '08 #5
On 2008-08-26 12:44:48 -0400, Christian Hackl <ha***@sbox.tugraz.atsaid:
red floyd wrote:
>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.

I think that's too narrow. An exception can be handled even if you are
not currently inside of a catch block, can't it?
It depends on just what you mean by "inside" a catch block. Most people
would say that the throw; below is inside a catch block. If you call
handleException directly from main, i.e. not inside any catch block,
the program will, indeed, be terminated.
>
#include <exception>
#include <stdexcept>
#include <iostream>

void handleException()
{
try
{
throw;
}
catch (std::exception const &exc)
{
std::cout << exc.what() << "\n";
}
catch (...)
{
std::cout << "unknown exception\n";
}
}

int main()
{
try
{
throw std::runtime_error("test");
}
catch (...)
{
handleException();
}
}

Isn't this a perfectly standard-conforming program? It does not call
terminate() when compiled and run with VC or GCC.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
13
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
7
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
1
by: djw | last post by:
c.l.p- I am having trouble understanding how one is supposed to correctly utilize try:...except:...finally: in real code. If I have a block of code like: def foo(): try: ... some code that...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
4
by: wk6pack | last post by:
Hi, I was wondering why when I declare the dim variable outside the try statement, I could use the .dispose() function but when I declare it inside the try statement, I get Name 'varname' is not...
3
by: Sori Schwimmer | last post by:
Hi, I think that would be useful to have an improved version of the "try" statement, as follows: try(retrys=0,timeout=0): # things to try except: # what to do if failed
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.