473,383 Members | 1,735 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,383 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 2589
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{
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.