Thread: Unhandled exceptions (throw with no try)

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Unhandled exceptions (throw with no try)

    I was under the impression that defining a throw, for instance on a class constructor, and then forcing an exception without a try-catch block would cause an unhandled exception. It would also cause an unhandled exception if I used the try block but the catch clauses didn't catch the specific error.

    However, on a simple test I did, my application exits and only outputs a message to the console, not generating an unhandled exception and consequently not firing my JIT debugger.

    This is my simple test:
    Code:
    #include <cstdlib>
    #include <stdexcept>
    
    class Range {
    public:
        explicit Range(const int i): sInt_(i) {
            if(i < 0 || i > 255)
                sInt_ = 0;
                throw std::out_of_range("Invalid small integer. Out of range.");
        }
        operator int() const { return sInt_; }
    private:
        size_t sInt_;
    };
    
    int main() {
    
        Range si(-4);
        
        return EXIT_SUCCESS;
    }
    Attached is my console output. Drmingw (the JITD I'm using) didn't fire.

    What gives?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    An exception that isn't caught will always terminate the program, and unless you implement your own exception class you can't really set the unexpected or terminate handlers anyway.

    I suppose the whole thing is a bit better written:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <stdexcept>
    
    class Range {
        size_t sInt_;
    public:
        explicit Range (int i = 0) try: sInt_(i) {
           if (i < 0 || i > 255)
              throw std::out_of_range("Invalid small integer for an object of type Range.");
        }
        catch ( std::out_of_range &oor ) {
            std::cerr << oor.what() << "\n";
            sInt_ = 0;
        }
        catch ( ... ) {
        }
    };
    
    int main() {
        Range si(-4);
    
        return EXIT_SUCCESS;
    }
    It is pretty difficult to keep the program running if a constructor fails (that's at a low level in the program), but at least your custom error message appears before you terminate.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A JIT debugger only gets called when JIT debugging is triggered. It is triggered, for example, by an unhandled SEH exception. (The keywords __try, __except etc. in VC++.) This is a Windows-specific mechanism that's used for quite a few things, among them memory protection faults and such. That's why accessing invalid memory would trigger the JITd.
    VC++ also uses SEH to implement C++ exceptions. Thus, an unhandled C++ exception becomes an unhandled SEH exception. The JITd is triggered.
    MinGW implements exceptions differently (using setjmp and longjmp, I think, but don't take my word on it) and thus an unhandled C++ exception does not trigger the JITd; it simply calls the unhandled exception handler, which in turn calls terminate, which in turn calls abort. The application exits without any further action, except the comment it writes.
    What you can do is write an unhandled exception handler that manually triggers JITd - I think code similar to this should suffice:
    Code:
    void my_unhandler()
    {
      asm ("int 3");
      std::terminate();
    }
    Use set_exception_handler (I think) to install this handler first thing in main(). The only problem is that it still won't catch exceptions thrown before main() starts, i.e. from constructors of global objects.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    That's very valuable information. Thanks both.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions "try, catch, and throw Statements" ???
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2008, 09:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. Unhandled Exceptions
    By nickname_changed in forum C# Programming
    Replies: 0
    Last Post: 03-13-2004, 08:00 PM
  5. causing default exceptions to throw additional info
    By lightatdawn in forum C++ Programming
    Replies: 14
    Last Post: 08-06-2003, 06:39 PM