Topic: Exceptions - why doesn't this work?


Author: "Bob Sanford" <Bob.Sanford@Compaq.com>
Date: 1997/07/19
Raw View
Hello.
Shouldn't this catch the thrown exception, since "runtime_error" is based
on "exception"? It never gets to my catch block. I'm using Borland C++
5.02.

#include <iostream>
#include <stdexcept>
using namespace std;

int main()
{
  try
    {
    throw runtime_error("Exception thrown.");
    }

  catch (exception x)
    {
    cout << x.what() << endl;
    return -1;
    }

  cout << "Everything is fine." << endl;
  return 0;
}

Thanks,
Bob Sanford
<Bob.Sanford@Compaq.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         ]
[ 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: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/07/20
Raw View
"Larry Brasfield" <larrybr@earthlink.net> writes:

>Bob Sanford <Bob.Sanford@Compaq.com> wrote in article
>> Shouldn't this catch the thrown exception, since "runtime_error" is based
>> on "exception"?

Yes, it should.

>runtime_error is derived from exception.  This means that
>a runtime_error reference (pointer or C++ reference) IsA
>exception reference.  Your program would do what you expect
>if you were to write the catch as I've indicated below.
...
>//>   catch (exception x)
>>   catch (exception & x)

That's true, but it ought to do what he expects even if he
catches by value rather than catching by reference.
See 15.3[except.handle]/3.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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: "Larry Brasfield" <larrybr@earthlink.net>
Date: 1997/07/20
Raw View
Bob Sanford <Bob.Sanford@Compaq.com> wrote in article
<01bc93c6$ceb027f0$256312ac@sys_bsanford>...
> Hello.
> Shouldn't this catch the thrown exception, since "runtime_error" is based
> on "exception"? It never gets to my catch block. I'm using Borland C++
> 5.02.

runtime_error is derived from exception.  This means that
a runtime_error reference (pointer or C++ reference) IsA
exception reference.  Your program would do what you expect
if you were to write the catch as I've indicated below.
[cut for length]

>   try
>     {
>     throw runtime_error("Exception thrown.");
>     }
>
//>   catch (exception x)
>   catch (exception & x)

[cut for length]

--
-- Larry Brasfield
The aforementioned views are mine alone.
---
[ 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                             ]