Re: c++ exception handling
From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 11/23/04
- Previous message: Jerry Coffin: "Re: Fstream increment."
- In reply to: Ole Andre Karlson: "c++ exception handling"
- Next in thread: Chris \( Val \): "Re: c++ exception handling"
- Reply: Chris \( Val \): "Re: c++ exception handling"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 23 Nov 2004 17:32:11 +0000
In article <Pine.LNX.4.61.0411231117190.31107@kaksi.ifi.uio.no>, Ole
Andre Karlson <oleaka@ifi.uio.no> wrote:
>
> I thought the correct way to throw/catch exceptions was this:
> ---------
> try {
> ...
> throw std::runtime_error("something");
> ...
> } catch (std::exception e) {
> std::cout << e.what() << std::endl;
> }
And you get the output of 'std::exception::what()', not that of
'std::runtime_error::what()'. 'what' is declared 'virtual', but its
'polymorphic' behaviour is inhibited because you have chosen to catch
the exception by value (i.e. 'std::exception' rather than
'std::exception&').
It is almost always better to catch exceptions by reference; since
standard exceptions are not mutable, it also makes sense to catch them
by 'const' reference.
The distinction between pass / catch by value and by reference is
fundamental in C++ and must be appreciated; in other languages, like
Java and C#, the issue does not arise.
Alwyn
- Previous message: Jerry Coffin: "Re: Fstream increment."
- In reply to: Ole Andre Karlson: "c++ exception handling"
- Next in thread: Chris \( Val \): "Re: c++ exception handling"
- Reply: Chris \( Val \): "Re: c++ exception handling"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|