Click here to Skip to main content
16,010,876 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: combining MFC and callback function Pin
_Magnus_3-Jul-02 3:46
_Magnus_3-Jul-02 3:46 
GeneralRe: combining MFC and callback function Pin
3-Jul-02 21:52
suss3-Jul-02 21:52 
Generalfile /open dialog filter in MDI Pin
ns3-Jul-02 3:24
ns3-Jul-02 3:24 
GeneralDeclaring function that throw exceptions Pin
Alexandru Savescu3-Jul-02 3:07
Alexandru Savescu3-Jul-02 3:07 
GeneralRe: Declaring function that throw exceptions Pin
Orbital^3-Jul-02 3:27
Orbital^3-Jul-02 3:27 
GeneralRe: Declaring function that throw exceptions Pin
Alexandru Savescu3-Jul-02 3:32
Alexandru Savescu3-Jul-02 3:32 
GeneralRe: Declaring function that throw exceptions Pin
Orbital^3-Jul-02 3:43
Orbital^3-Jul-02 3:43 
GeneralRe: Declaring function that throw exceptions Pin
Len Holgate3-Jul-02 22:14
Len Holgate3-Jul-02 22:14 
Alexpro wrote:
The question is:
Do you decorate your function declarations with an exception specification or don't bother since it does not matter. Why? Because it is good design? Also, if you do, do you use the throw (CMyException) both in the header and the implementation file?


Exception specifications in C++ are broken. If you specify a function as having an exception specification (and if the compiler conforms to the standard) then any exception that the function throws that isn't in the specification causes the unexpected exception handler to be called which defaults to terminating the program.

This is considerably different to how they work in Java. Java has static checking of exception specifications which means that you can't write a function that calls another function yet doesn't either handle the exceptions specificed in the other functions exception specification or declare that exception in its own exception specification. It's a complex problem to make a C++ compiler do this static checking as it would most probably need sight of all source used for a start as there's probably not enough information in the resulting object files. There's then the issue of whether it's a good idea to have it do that checking...

In Java it's often a pain to have to modify lots of code when a library changes its exception spec. It wouldnt be so bad if the callers actually did stuff with the new exception, but often they don't, they just pass it on up the chain. Of course if each library caught every exception and translated the appropriate ones into their own more descriptive exceptions then that wouldnt be so bad, but you can find yourself upgrading a library and suddenly having to deal with 'blah.blah.sublibrary.we.now.use.exception' which, as I said, is a pain. If everyone did the right thing it would possibly be a good thing and add value, but people, being people, dont, and it isn't.

So, my view is that in Java it's a pain and isn't used properly and in C++ it's a liability and should be avoided at all costs unless you want your programs to just terminate randomly. To write robust code in C++ using exception specs you would need to wrap all access to any code that you didnt have sight of the source of in a try/catch block which translated all exceptions into something you knew. If one developer fails to do that in one library anywhere in your source tree then your program may just decide to exit. To be really sure you'd have to wrap ALL code in such a block. This isn't really such a bad thing, it may look nice to have that kind of rule on your coding standard, but IMHO it sucks Smile | :) Part of the nice thing about exceptions is that you dont need to fill each function in the call stack with error handling gunk which obscures the purpose of the real code. Most of the time you can let exceptions flow up through your code unobstructed. If you expect them, and can deal with them here, fine, you can do so. If you dont then as long as you deal with them at key places (COM interface boundaries, thread/process boundaries, etc) you can write robust code that handles even the most unexpected of exception in a graceful and recoverable (well you can try) manner.

In VC++ at present it's even worse than that Wink | ;) as it doesn't enforce them, so they may as well just be a comment with interesting syntax and you really don't want to get me started on comments Wink | ;)

Len Holgate
www.jetbyte.com
The right code, right now.
GeneralCharacter Sets Pin
Matthew R. Miller3-Jul-02 2:23
Matthew R. Miller3-Jul-02 2:23 
GeneralProbelm in reading images Pin
SamirSood3-Jul-02 1:39
SamirSood3-Jul-02 1:39 
GeneralGDI+ and MFC Pin
Williams3-Jul-02 1:38
Williams3-Jul-02 1:38 
GeneralRe: GDI+ and MFC Pin
Christian Graus3-Jul-02 1:53
protectorChristian Graus3-Jul-02 1:53 
QuestionWhere can I find a DB2 CLI function C++ class wrapper? Pin
wabc3-Jul-02 0:51
wabc3-Jul-02 0:51 
AnswerRe: Where can I find a DB2 CLI function C++ class wrapper? Pin
DiWa4-Jul-02 0:13
DiWa4-Jul-02 0:13 
GeneralList Control with Printing Support Pin
Renjith Ramachandran3-Jul-02 0:16
Renjith Ramachandran3-Jul-02 0:16 
GeneralRe: List Control with Printing Support Pin
surbinsho3-Jul-02 2:03
surbinsho3-Jul-02 2:03 
GeneralRe: List Control with Printing Support Pin
Ravi Bhavnani3-Jul-02 3:41
professionalRavi Bhavnani3-Jul-02 3:41 
GeneralRe: List Control with Printing Support Pin
surbinsho3-Jul-02 18:27
surbinsho3-Jul-02 18:27 
GeneralRe: List Control with Printing Support Pin
surbinsho4-Jul-02 19:07
surbinsho4-Jul-02 19:07 
GeneralGetting enter keypress in a control Pin
Chou3-Jul-02 0:06
Chou3-Jul-02 0:06 
GeneralRe: Getting enter keypress in a control Pin
Nish Nishant3-Jul-02 0:57
sitebuilderNish Nishant3-Jul-02 0:57 
GeneralReverse Engineering Pin
super2-Jul-02 23:48
professionalsuper2-Jul-02 23:48 
GeneralRe: Reverse Engineering Pin
Christopher Duncan3-Jul-02 12:15
Christopher Duncan3-Jul-02 12:15 
GeneralRe: Reverse Engineering Pin
super3-Jul-02 18:25
professionalsuper3-Jul-02 18:25 
GeneralRe: Reverse Engineering Pin
Christopher Duncan4-Jul-02 4:13
Christopher Duncan4-Jul-02 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.