472,782 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 2533
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.