Re: c++ exception handling

From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 11/23/04

  • Next message: Ditoa: "Re: Learning C++"
    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


  • Next message: Ditoa: "Re: Learning C++"

    Relevant Pages

    • Re: some c# questions!! please send me the answer
      ... TryParse first to see if the string is a valid DateTime before doing ... In .NET 1.1 all you can do is Prase and catch any exceptions ... Boxing is creating a "reference" shell around a value object so that it ... each "int" and then adds those object wrappers to the ...
      (microsoft.public.dotnet.csharp.general)
    • Re: what would you like to see in a 2nd edition Nutshell?
      ... but as a quick reference it is tricky; ... I beg you to include a ONE-PAGE table of exceptions that shows the ... inheritance hierarchy via indentation (e.g. like Python Essential ... The detailed info will probably have to follow. ...
      (comp.lang.python)
    • Re: Separating People
      ... make that chage it will affect a couple dozen queries and reports that would ... but couldn't find a reference. ... downloaded before the server got rid of it, or maybe the news server I use ... you will need a human to go back through and fix all the exceptions. ...
      (microsoft.public.access.queries)
    • RE: The OutputPath property is not set for this project - interest
      ... I ran into this one time before when installing Compact Framework SP1 (maybe ... attempted to compile it, ... When choosing to "Add Reference" for a project we would normally click on ... everything compiles again and the exceptions are gone. ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: NullReferenceException Message Feature Request
      ... I havn't been able to reproduce it in a debug environment. ... Please tell me you have exceptions set to 'break' in the debugger.... ... it's usually absolutely obvious what the null reference should have ... >> Bryan Livingston ...
      (microsoft.public.dotnet.framework.clr)