Topic: lib.std.exceptions


Author: polk@sprintmail.com (Max Polk)
Date: 1997/11/17
Raw View
I have a question about the exception classes in the Standard C++
Library:
----------------------------------------------------------------------
19.1  Exception classes       [lib.std.exceptions]

1 The Standard C++ library provides classes to be used to report certain
  errors (_lib.res.on.exception.handling_)  in  C++  programs.   In  the
  error  model  reflected  in these classes, errors are divided into two
  broad categories: logic errors and runtime errors.
...
  namespace std {
    class logic_error;
      class domain_error;
      class invalid_argument;
      class length_error;
      class out_of_range;
    class runtime_error;
      class range_error;
      class overflow_error;
      class underflow_error;
  }

  19.1.1  Class logic_error     [lib.logic.error]

 namespace std {
    class logic_error : public exception {
    public:
      logic_error(const string& what_arg);
    };
  }

1 The class logic_error defines the type of objects thrown as exceptions
  to  report  errors  presumably detectable before the program executes,
  such as violations of logical preconditions or class invariants.
----------------------------------------------------------------------

Why were these new exception-derived classes added?  Is the idea
that we should all be throwing an object of one of these types
when a logic or runtime error occurs?  If so, why?  Is it
supposed to ease setting up a limited set of catch blocks?  Or is
it to keep us all in line to ease maintenance and readability?

And for logic_error, what does 19.1.1 mean when it says
"detectable before the program executes?"  How can an exception
be thrown BEFORE the program executes?  How can a logic_error
exception occur at any time earlier than when the code containing
the throw of the logic_error is executed?
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/11/17
Raw View
Max Polk wrote:
>
> And for logic_error, what does 19.1.1 mean when it says
> "detectable before the program executes?"  How can an exception
> be thrown BEFORE the program executes?  How can a logic_error
> exception occur at any time earlier than when the code containing
> the throw of the logic_error is executed?

I think that's a bad description of the distinction between logic and runtime
errors. Logic errors are those that could have been prevented by a simple
run-time test, so imply that the programmer left something out. Runtime errors
are those that cannot (or cannot easily) be prevented by a simple runtime test.

For instance, a square root function would throw domain_error if given a
negative number, implying that the programmer could have prevented it with a
simple if statement. However, a function that overflows would throw
range_error, because detecting that in advance is generally not possible, short
of duplicating much of the code in the function. (Consider what it would take
to find out in advance whether adding two floats together will overflow,
without actually adding them.)

--

Ciao,
Paul
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Pete Becker <petebecker@acm.org>
Date: 1997/11/17
Raw View
Max Polk wrote:
>
> Why were these new exception-derived classes added?  Is the idea
> that we should all be throwing an object of one of these types
> when a logic or runtime error occurs?

It provides a convenient hierarchy for derived objects.

> If so, why?  Is it
> supposed to ease setting up a limited set of catch blocks?

Yup.

> Or is
> it to keep us all in line

Nope.

> to ease maintenance and readability?

Yup.

>
> And for logic_error, what does 19.1.1 mean when it says
> "detectable before the program executes?"

sqrt(-2.0) is wrong. It's a logic error.

> How can an exception
> be thrown BEFORE the program executes?>

It can't. The idea of logic_error is that it reflects an error that
could have been detected by examining the program logic.
 -- Pete
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]