Topic: Deriving from standard exceptions
Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: Mon, 7 Jan 2002 21:36:43 GMT Raw View
"apm" <apm35@student.open.ac.uk> wrote in message
news:d1a33011.0201032342.20c0ba4@posting.google.com...
> "Carl Daniel" <cpdaniel@pacbell.net> wrote in message
news:<MhJY7.20768$mu6.320490558@newssvr21.news.prodigy.com>...
> [snip]
> > > However, I can also see how building on the standard exception
hierarchy
> > > avoids creating yet-another-set-of-exception classes.
> >
> > I think it's a "good thing", although I will confess to have always
resorted
> > to making my own root exception class (due to compiler or library issues
> > with deriving from the standard exception classes).
> >
> > -cd
>
> What issues were those ? Perhaps they form a valid reason for not
> inheriting from the standard exception base class ?
>
IIRC, in some cases it just didn't work (some library problem I didn't have
time to debug). In more recent cases, it was, for example, becuase I needed
the exception text (i.e. what()) to be Unicode, not ANSI.
-cd
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: jk@steel.orel.ru (Eugene Karpachov)
Date: Thu, 3 Jan 2002 18:06:10 GMT Raw View
Wed, 2 Jan 2002 18:58:37 GMT Scott Robert Ladd =CE=C1=D0=C9=D3=C1=CC:
> #include <stdexcept>
>
> class my_error : public runtime_error
> {
> // yada, yada, nada
> };
>
>My initial feeling is that the code above is WRONG, because it implied t=
hat
>my_error is an "offocial" exception sanctioned by the Standard. Someone
Yes, it is exactly what is written - "my_error" ISA runtime_error. Why
is it bad isn't clear for me.
>using "catch (runtime_error)" would also catch my exception, which may n=
ot
>be what they expect.
It is just what public inheritance is for. They might expect to catch
_all_ runtime_errors, including "my_error", "his_error" if it is "public
runtime_error" etc.
--=20
jk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Thu, 3 Jan 2002 18:12:11 GMT Raw View
"James Kuyper Jr." <kuyper@wizard.net> wrote:
> My understanding is that one of the main purposes of defining the
> standard exception classes was to allow users to derive from them. I
> can't prove that with any explicit citations, but it seems to me to be
> implicit in the amount of detail that the standard gives them.
Agreed.
Along that line of thinking, I note that std::exception defines a virtual
destructor; the existence of such strongly suggests the intention for
user-defined derivation.
--
Scott Robert Ladd
Master of Complexity, Destroyer of Order and Chaos
Visit CoyoteGulch at http://www.coyotegulch.com
No ads -- just info, algorithms, and very free code.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Thu, 3 Jan 2002 19:15:13 GMT Raw View
"Pete Becker" <petebecker@acm.org> wrote
> That is exactly what they are intended for.
I think James Kuyper's response mirrors my own confusion on this issue, and
may explain why I haven't seen many examples of deriving from standard
exceptions. I think the Standard would do well to include some information
on *intent*, to guide programmers on their way.
As it is, I'll be building on the std::exception hierarchy from this point
forward.
--
Scott Robert Ladd
Master of Complexity, Destroyer of Order and Chaos
Visit CoyoteGulch at http://www.coyotegulch.com
No ads -- just info, algorithms, and very free code.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: cmd@gmrc.gecm.com (Chris Dearlove)
Date: Thu, 3 Jan 2002 19:45:17 GMT Raw View
Randy Charles Morin (rmorin@kbcafe.com) wrote:
: I disagree. I think that you should try to derive all exceptions
: from the STL exception class or classes derived from the STL
: exception class. My opinion. I wish Stroustrup would answer this query.
See C++PL 3rd ed. page 385, below the hierarchy figure. I'm not going
to quote all of it, so you could fairly say it's not fully in context,
but it finishes "Some people view this as a useful framework for all
errors and exceptions; I don't."
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: apm35@student.open.ac.uk (apm)
Date: Fri, 4 Jan 2002 21:48:47 GMT Raw View
"Carl Daniel" <cpdaniel@pacbell.net> wrote in message news:<MhJY7.20768$mu6.320490558@newssvr21.news.prodigy.com>...
[snip]
> > However, I can also see how building on the standard exception hierarchy
> > avoids creating yet-another-set-of-exception classes.
>
> I think it's a "good thing", although I will confess to have always resorted
> to making my own root exception class (due to compiler or library issues
> with deriving from the standard exception classes).
>
> -cd
What issues were those ? Perhaps they form a valid reason for not
inheriting from the standard exception base class ?
Regards,
Andrew M.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Wed, 2 Jan 2002 18:58:37 GMT Raw View
Is is "good" or "bad" practice to derive an exception class from a Standard
C++ exception? Or, by way of a code example:
#include <stdexcept>
class my_error : public runtime_error
{
// yada, yada, nada
};
My initial feeling is that the code above is WRONG, because it implied that
my_error is an "offocial" exception sanctioned by the Standard. Someone
using "catch (runtime_error)" would also catch my exception, which may not
be what they expect.
However, I can also see how building on the standard exception hierarchy
avoids creating yet-another-set-of-exception classes.
So what do people here think?
--
Scott Robert Ladd
Master of Complexity, Destroyer of Order and Chaos
Visit CoyoteGulch at http://www.coyotegulch.com
No ads -- just info, algorithms, and very free code.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: Wed, 2 Jan 2002 19:46:40 GMT Raw View
"Scott Robert Ladd" <scott@coyotegulch.com> wrote in message
news:aDFY7.242770$oj3.43095778@typhoon.tampabay.rr.com...
> Is is "good" or "bad" practice to derive an exception class from a
Standard
> C++ exception? Or, by way of a code example:
>
> #include <stdexcept>
>
> class my_error : public runtime_error
Of course, you mean
public std::runtime_error
> {
> // yada, yada, nada
> };
>
> My initial feeling is that the code above is WRONG, because it implied
that
> my_error is an "offocial" exception sanctioned by the Standard. Someone
> using "catch (runtime_error)" would also catch my exception, which may not
> be what they expect.
>
It's not an "official" exception unless you also insert in into namespace
std. Of course, a catch() can't tell what namespace your exception resides
in...
> However, I can also see how building on the standard exception hierarchy
> avoids creating yet-another-set-of-exception classes.
I think it's a "good thing", although I will confess to have always resorted
to making my own root exception class (due to compiler or library issues
with deriving from the standard exception classes).
-cd
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 3 Jan 2002 05:15:50 GMT Raw View
Scott Robert Ladd wrote:
>
> Is is "good" or "bad" practice to derive an exception class from a Standard
> C++ exception? Or, by way of a code example:
>
> #include <stdexcept>
>
> class my_error : public runtime_error
> {
> // yada, yada, nada
> };
>
> My initial feeling is that the code above is WRONG, because it implied that
> my_error is an "offocial" exception sanctioned by the Standard. Someone
> using "catch (runtime_error)" would also catch my exception, which may not
> be what they expect.
My understanding is that one of the main purposes of defining the
standard exception classes was to allow users to derive from them. I
can't prove that with any explicit citations, but it seems to me to be
implicit in the amount of detail that the standard gives them. The
standard gives far more detail about them than would be needed if they
were meant to be purely internal. It wouldn't have to identify them at
all, for that purpose - each implementation could be free to use it's
own names. On the other hand, it gives far too little information to do
much of anything with them, if they weren't intended to be available for
use as base classes. In particular, several of the exception classes
have not a single function in the entire standard library which is
defined as using them, either to throw() or to catch().
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Pete Becker <petebecker@acm.org>
Date: Thu, 3 Jan 2002 05:16:15 GMT Raw View
Scott Robert Ladd wrote:
>
> Is is "good" or "bad" practice to derive an exception class from a Standard
> C++ exception? Or, by way of a code example:
>
> #include <stdexcept>
>
> class my_error : public runtime_error
> {
> // yada, yada, nada
> };
>
That is exactly what they are intended for.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Randy Charles Morin" <rmorin@kbcafe.com>
Date: Thu, 3 Jan 2002 15:32:58 GMT Raw View
I disagree. I think that you should try to derive all exceptions from the STL exception class or classes derived from the STL
exception class. My opinion. I wish Stroustrup would answer this query.
--
Randy Charles Morin
http://www.kbcafe.com
Feel free to contact me by private email or messenger
morin_randy@hotmail.com
randy@kbcafe.com
"Scott Robert Ladd" <scott@coyotegulch.com> wrote in message news:aDFY7.242770$oj3.43095778@typhoon.tampabay.rr.com...
> Is is "good" or "bad" practice to derive an exception class from a Standard
> C++ exception? Or, by way of a code example:
>
> #include <stdexcept>
>
> class my_error : public runtime_error
> {
> // yada, yada, nada
> };
>
> My initial feeling is that the code above is WRONG, because it implied that
> my_error is an "offocial" exception sanctioned by the Standard. Someone
> using "catch (runtime_error)" would also catch my exception, which may not
> be what they expect.
>
> However, I can also see how building on the standard exception hierarchy
> avoids creating yet-another-set-of-exception classes.
>
> So what do people here think?
>
> --
> Scott Robert Ladd
> Master of Complexity, Destroyer of Order and Chaos
> Visit CoyoteGulch at http://www.coyotegulch.com
> No ads -- just info, algorithms, and very free code.
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]