473,320 Members | 2,027 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,320 software developers and data experts.

C++ Exception handling

Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.

Mar 3 '06 #1
13 2585
On 2006-03-03, ju******@gmail.com <ju******@gmail.com> wrote:
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.


The feature itself isn't that complicated, it's just using them
correctly that is hard. They are rather like a unicycle in that way.

When designing objects with contructors that might fail, exceptions
become quite useful. The other option is to return invalid objects and
hope nobody uses them.

--
Neil Cerutti
Mar 3 '06 #2
On 3 Mar 2006 09:48:15 -0800, ju******@gmail.com wrote:
Is C++ Exception handling useful? think it is too complicated.
Exception handling combined with RAII (the most important C++ idiom)
greatly simplifies programming. In C++ exception handling is
complicated only if you disregard RAII and try to use it in
Java-style.
What kinds of project need to use it?


All except 'Hello world'.

Best wishes,
Roland Pibinger
Mar 3 '06 #3

ju******@gmail.com wrote:
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.


Yes, the C++ exception handling mechanism is extremely useful.

It is a more robust method for handling errors than fastidiously
checking for error codes from functions that return 0 on success, and
some non-zero value on failure. This kind of error code checking is
tedious and obscures your program logic.

The C++ Exception Model:
* destructors are invoked for all live objects as the stack of function
calls "unwinds."
* exception specifications specify what type of exception(s) a function
will throw.
* termination vs. resumption semantics.

I wouldn't consider exception handling too complicated. You need to
understand:
* the syntax to implement.
* what happens when an exception is thrown.
* how to handle the exception.

Hope this helps...

Mike

-----
ACGNJ Java Users Group
http://www.javasig.org/

Mar 3 '06 #4

Michael Redlich wrote:
ju******@gmail.com wrote:
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.


The C++ Exception Model:
* destructors are invoked for all live objects as the stack of function
calls "unwinds."
* exception specifications specify what type of exception(s) a function
will throw.


Unfortunately, that's not quite what exception specifications do, since
there's no way in general for the exception specification to be
enforced by the compiler. They are generally regarded as the one part
of C++ exception handling that is largely useless.

http://www.gotw.ca/gotw/082.htm
http://www.boost.org/more/lib_guide....-specification

Gavin Deane

Mar 4 '06 #5
On 4 Mar 2006 00:58:06 -0800, "Gavin Deane" <de*********@hotmail.com>
wrote:
Michael Redlich wrote:
ju******@gmail.com wrote:
> Is C++ Exception handling useful? think it is too complicated. What
> kinds of project need to use it? Thanks.
The C++ Exception Model:
* destructors are invoked for all live objects as the stack of function
calls "unwinds."
* exception specifications specify what type of exception(s) a function
will throw.


Unfortunately, that's not quite what exception specifications do, since
there's no way in general for the exception specification to be
enforced by the compiler.


Unlike Java, C++ exception specification are supposed to be enforced
by _you_, the programmer, not the compiler. You (can) guarantee that
only the specified exception(s) may be thrown from a function.
They are generally regarded as the one part
of C++ exception handling that is largely useless.
Nope, they are useful especially for high level functions and
interfaces. They tighten the 'contract' between implementor and user.
They are just not like Java exception specifications.
http://www.gotw.ca/gotw/082.htm
http://www.boost.org/more/lib_guide....-specification


Stroustrup "The C++ Programming Language" 14.6

Best wishes,
Roland Pibinger
Mar 4 '06 #6

Roland Pibinger wrote:
On 4 Mar 2006 00:58:06 -0800, "Gavin Deane" <de*********@hotmail.com>
wrote:
Michael Redlich wrote:
ju******@gmail.com wrote:
> Is C++ Exception handling useful? think it is too complicated. What
> kinds of project need to use it? Thanks.

The C++ Exception Model:
* destructors are invoked for all live objects as the stack of function
calls "unwinds."
* exception specifications specify what type of exception(s) a function
will throw.


Unfortunately, that's not quite what exception specifications do, since
there's no way in general for the exception specification to be
enforced by the compiler.


Unlike Java, C++ exception specification are supposed to be enforced
by _you_, the programmer, not the compiler. You (can) guarantee that
only the specified exception(s) may be thrown from a function.


I (can) also make mistakes.
They are generally regarded as the one part
of C++ exception handling that is largely useless.


Nope, they are useful especially for high level functions and
interfaces. They tighten the 'contract' between implementor and user.


How? Since the exception specification is not enforced by the compiler,
how is it anything more than documentation of what the function is
supposed to do?

If the function really will only ever throw the specified exceptions,
then moving the exception specification from the documentation to the
code adds nothing.

Gavin Deane

Mar 4 '06 #7
On 4 Mar 2006 02:42:26 -0800, "Gavin Deane" <de*********@hotmail.com>
wrote:
Roland Pibinger wrote:
>They are generally regarded as the one part
>of C++ exception handling that is largely useless.
Nope, they are useful especially for high level functions and
interfaces. They tighten the 'contract' between implementor and user.


How? Since the exception specification is not enforced by the compiler,


They are enforced in the code by the implementor. That's the point.
The user can rely on the exception specification.
how is it anything more than documentation of what the function is
supposed to do?


If your exception specification is not correct then it's a bug in your
code (that probably crashes the program). That's much more than 'just'
documentation. BTW, if you know which exception can be thrown you can
specifiy it in code _and_ documentation. If you don't know you can do
neither.

Best wishes,
Roland Pibinger
Mar 4 '06 #8

Roland Pibinger wrote:
On 4 Mar 2006 02:42:26 -0800, "Gavin Deane" <de*********@hotmail.com>
wrote:
Roland Pibinger wrote:
>They are generally regarded as the one part
>of C++ exception handling that is largely useless.

Nope, they are useful especially for high level functions and
interfaces. They tighten the 'contract' between implementor and user.
How? Since the exception specification is not enforced by the compiler,


They are enforced in the code by the implementor. That's the point.
The user can rely on the exception specification.


If some behaviour is only enforced in the code by the implementor, then
anything that communicates the nature of that behaviour to other
programmers is documentation. C++ provides a tool for that - comments.
how is it anything more than documentation of what the function is
supposed to do?


If your exception specification is not correct then it's a bug in your
code (that probably crashes the program).


Or it's a bug in the exception specification itself. If I modify the
implementation of a function so that instead of, for example, doing all
its processing in memory, it temporarily reads and writes to a file,
but I fail to add my file IO exception to the documentation, one of two
things could happen. If I have documented the exceptions thrown by
writing a comment then I have confused maintenance programmers (a bad
thing). If I have documented the exceptions thrown by writing an
exception specification then I have broken the program (a worse thing).

The use of exception specifications has made my program more brittle.
That's much more than 'just'
documentation. BTW, if you know which exception can be thrown you can
specifiy it in code _and_ documentation. If you don't know you can do
neither.


I don't think writing the documentation in two places is a good idea.

Gavin Deane

Mar 4 '06 #9
On 3 Mar 2006 09:48:15 -0800, ju******@gmail.com wrote:
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.

"Too complicated" compared to what? All alternatives that can solve
the same problem as the exception concept are more complicated, clumsy
etc.

Please do not compare a program that uses EH with one that doesn't.

Martin
Mar 5 '06 #10
Gavin Deane <de*********@hotmail.com> wrote:
Unfortunately, that's not quite what exception specifications do, since
there's no way in general for the exception specification to be
enforced by the compiler. They are generally regarded as the one part
of C++ exception handling that is largely useless.

http://www.gotw.ca/gotw/082.htm
http://www.boost.org/more/lib_guide....-specification


Here's another article that basically summarizes to "Don't use exception
specifications":

http://www.gotw.ca/publications/mill22.htm

--
Marcus Kwok
Mar 6 '06 #11
Geo

Roland Pibinger wrote:
On 4 Mar 2006 02:42:26 -0800, "Gavin Deane" <de*********@hotmail.com>
wrote:
Roland Pibinger wrote:
If your exception specification is not correct then it's a bug in your
code (that probably crashes the program). That's much more than 'just'
documentation. BTW, if you know which exception can be thrown you can
specifiy it in code _and_ documentation. If you don't know you can do
neither.


But int the presence of function objects, call back functions,
polymorphism or templates, it's almost impossible to make your
exception specifications correct, and therefore almost guaranteeing
that your program *will* crash.

Mar 6 '06 #12
On 6 Mar 2006 07:45:28 -0800, "Geo" <gg@remm.org> wrote:

But int the presence of function objects, call back functions,
polymorphism or templates, it's almost impossible to make your
exception specifications correct, and therefore almost guaranteeing
that your program *will* crash.


You mean you cannot safely write a function that complies to an
exception specification?

void foo() throw (MyException) {
try {

// use function objects, call back functions,
// polymorphism or templates
} catch (MyException& e) {
throw;
} catch (std::exception& e) {
throw MyException (e);
} catch (...) {
throw MyException ("unknown exception in foo()");
}
}

For a generalized 'ExceptionFilter' see
http://article.gmane.org/gmane.comp....t.devel/132551

Best wishes,
Roland Pibinger
Mar 6 '06 #13
Geo

Roland Pibinger wrote:
On 6 Mar 2006 07:45:28 -0800, "Geo" <gg@remm.org> wrote:

But int the presence of function objects, call back functions,
polymorphism or templates, it's almost impossible to make your
exception specifications correct, and therefore almost guaranteeing
that your program *will* crash.


You mean you cannot safely write a function that complies to an
exception specification?

void foo() throw (MyException) {
try {

// use function objects, call back functions,
// polymorphism or templates
} catch (MyException& e) {
throw;
} catch (std::exception& e) {
throw MyException (e);
} catch (...) {
throw MyException ("unknown exception in foo()");
}
}

For a generalized 'ExceptionFilter' see
http://article.gmane.org/gmane.comp....t.devel/132551

Best wishes,
Roland Pibinger


But what would be the point of that, you've just localised the
exception handling, destroying the real advantage of exceptions
(dealing with them at the most appropriate level) just to satisfy a
pointless exception spec. It would have been much cleaner and less
error prone to just have omitted the exception spec. How useful is
you're 'unknown' exception, after all the original exception probably
was/is known, just not to you're catch block, lost information for no
gain.

Mar 7 '06 #14

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

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.