473,660 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2624
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*********@ho tmail.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.


Unfortunatel y, 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*********@ho tmail.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.


Unfortunatel y, 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*********@ho tmail.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*********@ho tmail.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

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

Similar topics

11
2869
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
2335
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 assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't catch the exception and keep going. I'd just get the error message and then it would shut the...
7
5992
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2745
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 exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
2
2591
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 Layer. My questions is where should I put exception handling: 1) Should it be put in all significant methods in all layers? 2) Should I create an exception base class that will handle the errors and pass useful error messages to the user?
9
2534
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
44
4207
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
5129
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 .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
41
3048
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 handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out three handlers that all do the same thing. So, I could just catch Exception. But, is that...
1
3101
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
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.