Re: use exception in default catch(...)
Al wrote:
Seungbeom Kim wrote:
Al wrote:
Right now you have to use a whole stack of catch's to achieve a similar
(but incomplete) effect, and it is much more verbose. Languages like C#
or Java don't need this because everything that can be thrown has a
common base. Since C++ doesn't have that, it should allow better catch
support.
So, what does the common base support? Doesn't std::exception do a
similar thing? (Or you could define your own common base, of course.)
Not that you can prevent an int or a const char* being thrown, but I
don't expect any decent code to throw anything other than a descendant
of std::exception. (And if /you/ do it yourself, you know what types can
possibly be thrown...)
Well, I wouldn't expect decent code to do it, but it might, and I've
seen it happen. It would be a lot more convenient to have the compiler
help you with this.
I think help is coming.
In C#, the Exception class is very much like std::exception, but more
powerful. Everything that is thrown must derive from it, so:
catch (Exception e) {}
Catches everything (threadwise). In addition, an Exception object
carries very useful things like a message string, a detailed stack
trace, the source of the exception, an optional sub-exception if you
want to chain exceptions (this could be useful for throwing destructors,
I think), etc.
I'm not sure when the stack trace would be of any use. My
experience with Java was about the only time you might use it is
in cases where you really would prefer to crash anyway.
I never understood why C++ allows you to throw an int, or even a
pointer, for instance. What's /that/ about?
It's an elegant form of calling exit, if you adopt the
convention. Calling exit does not call destructors of on stack
objects; throwing an int does. I use it for fatal errors,
throwing the int in the error handling routine, and catching it
in main and returning it. Destructors take care of deleting
incompletely written or temporary files, etc.
It's probably not a good idiom for long running programs, like
servers or GUI programs, but I've found it fairly useful for
command line utility programs.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]