0

hey , i am trying to inherit the exception class and make a new class called NonExistingException: i wrote the following code in my h file:

class NonExistingException : public exception
{
public:
    virtual const char* what() const throw()  {return "Exception: could not find 
     Item";}
};

in my code before i am sending something to a function i am writing

try{
    func(); // func is a function inside another class
}
catch(NonExistingException& e)
{
    cout<<e.what()<<endl;
}
catch (exception& e)
{
     cout<<e.what()<<endl;
}

inside func i am throwing an exception but nothing catches it. thanks in advance for your help.

14
  • How are you throwing? It should look like this: throw NonExistingVehicleException(); Commented Sep 22, 2010 at 8:35
  • 3
    Note: Best to catch by const reference. Commented Sep 22, 2010 at 8:36
  • You must throw the exception as "throw NonExistingException()", not "throw new NonExistingException()". Commented Sep 22, 2010 at 8:36
  • 1
    Can you show us the code where you actually throw the exception? Commented Sep 22, 2010 at 8:37
  • 1
    @ sellibitze: Either one will do. You obviously need to read them :-p (Joke) But no it means I am too tiered to think about it and you should read a book about the subject. But Frankly the whole point of C++ is Type and half that battle is getting const correctness done properly. Commented Sep 22, 2010 at 9:29

1 Answer 1

5

I would do this:

// Derive from std::runtime_error rather than std::exception
// runtime_error's constructor can take a string as parameter
// the standard's compliant version of std::exception can not
// (though some compiler provide a non standard constructor).
//
class NonExistingVehicleException : public std::runtime_error
{
    public:
       NonExistingVehicleException()
         :std::runtime_error("Exception: could not find Item") {}
};

int main()
{
    try
    {
        throw NonExistingVehicleException();
    }
    // Prefer to catch by const reference.
    catch(NonExistingVehicleException const& e)
    {
        std::cout << "NonExistingVehicleException: " << e.what() << std::endl;
    }
    // Try and catch all exceptions
    catch(std::exception const& e)
    {
        std::cout << "std::exception: " << e.what() << std::endl;
    }
    // If you miss any then ... will catch anything left over.
    catch(...)
    {
        std::cout << "Unknown exception: " << std::endl;
        // Re-Throw this one.
        // It was not handled so you want to make sure it is handled correctly by
        // the OS. So just allow the exception to keep propagating.
        throw;

        // Note: I would probably re-throw any exceptions from main
        //       That I did not explicitly handle and correct.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.