Thread: I can't figure out why none of my catches are catching the thrown exception

  1. #1
    Registered User
    Join Date
    Mar 2024
    Posts
    4

    I can't figure out why none of my catches are catching the thrown exception

    I'm following a free course that uses C++98 by default. I installed VS Code on windows, along with MSYS. I can't seem to get C++98 to work on it because rather than telling me anything useful, it just says that using throw(type) is deprecated in C++17. So, I used g++ on the command prompt with -std=c++98, and it works. Then I run the exe, and it terminates, just like with the 2 online IDEs I've tried. I've tried the cpp.sh and Ideone online IDEs.

    It seems that no matter what catch I use and no matter where I place the try, catch statements, a logic_error exception isn't caught, and it terminates the program.

    I cannot for the life of me figure out why.

    This is one of their test questions. The question is what happens when this code is run? Well, it seems like the answer must be it terminates the program, but I don't know why. I like to solve the why so I know why the code is doing it. But I cannot catch the exception to see where it's able to skip all the catch statements that I've put everywhere!

    I know the answer is very simple, but I don't know what it is.

    Code:
    #include <iostream>
    #include <exception>
    #include <stdexcept>
    
    using namespace std;
    
    class X : public logic_error 
    {
        public:
            X() : logic_error("logic_error in X class")  {}
            
    };
    
    void z() throw(X)
    {
        throw new logic_error("logic_error in z()");
    }
    
    int main() 
    {
        try {
            
            X x;
            
            try{
                z();
            }
            catch(X &i) { cout << i.what(); }
            catch(exception &a) { cout << "This is catch(exception &a): " << a.what(); }
            catch(logic_error &lr) { cout << "This is catch(logic_error &lr): " << lr.what(); }
            catch(...) { cout << "This is catch(...)"; }
        }
        catch(...) { cout << "try surrounds all code in main except the return, this catch is at the bottom of that."; }
            
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2024
    Posts
    4
    Well, I figured out the problem finally! It's the new keyword. When you use new, it has to be a pointer, and class X is a child class to logic_error. So, logic_error cannot be an X, but X is a logic_error because a parent cannot be its child, but its child is its parent. I could have sworn I read something in the official C++ website that I can't remember at the moment that said that pointers were only kept around because of something to do with the fact that C++ was built on C. It distinctively made it sound like they would have deprecated pointers and only used references in C++ if they could have. You'd think they'd try to make pointers as unimportant as they can, but no, they're still quite vital to properly running C++, aren't they?

    So, to get it to work correctly, I needed to include logic_error* as potential throw.

    Minimally, I needed to change this:
    Code:
    void z() throw(X)
    
    {
        throw new logic_error("logic_error in z()");
    
    }
    into this:

    Code:
    void z() throw(X,logic_error*)
    
    {
        throw new logic_error("logic_error in z()");
    
    }
    It couldn't go anywhere because it was being blocked by that throw(X) permission statement that was looking for thrown class X data, not its parent data of class logic_error. And new meant it needed to be looking for a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-12-2012, 04:38 PM
  2. Illegal CrossThread Exception thrown when proccess is long
    By kairozamorro in forum Windows Programming
    Replies: 14
    Last Post: 10-22-2009, 07:53 PM
  3. Replies: 5
    Last Post: 09-17-2009, 11:31 AM
  4. How do I catch an exception thrown from a linked DLL?
    By 6tr6tr in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 07:49 AM
  5. Where the exception was thrown?
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2007, 12:53 PM

Tags for this Thread