smallB
smallB

Reputation: 17110

Catch block doesn't want to catch thrown exception

I have a code:

void Engine::count(const std::set<boost::filesystem3::path>& files)
{
    for (auto e : files)
    {
        try
        {
            count_(e);
        }
        catch (const Bad_formatting& ) // Here I'm trying to catch this exception
        {//but debugger never stops on this line even though breakpoint is set
            throw;                     // and re-throw it
        }
    }
} 

Then there is this count_ function:

void Engine::count_(const boost::filesystem3::path& file)
{
    // and somewhere along this lines I'm throwing Bad_Formatting: 

    if (something)
    {

    }
    else
    {
        throw Bad_formatting(file,"No end of multicomment found.");
    }
}

But after throwing this exception, I'm getting dialog telling me that my application requested runtime to terminate in an unusual way...
The exception is never cought. Why? Does the fact that both of those fncs are static has anything to do with it? Or the fact that I'm using Qt?
EDIT:
This is the code which calls count:

try
    {

            Engine::count(files);



    }
    catch (const Bad_formatting& e)
    {

        QMessageBox::warning(nullptr,"Bad Formatting",msg);
    }  
//// 



struct Bad_formatting : public std::runtime_error
{
private:
    boost::filesystem3::path file_name_;

public:
    Bad_formatting(boost::filesystem3::path file_name,
                   const char* msg):std::runtime_error(msg),
                                    file_name_(file_name)
    {

    }

    const boost::filesystem3::path& file_name()const
    {
        return file_name_;
    }
    ~Bad_formatting()throw()
    {/*eb*/}
};

Upvotes: 0

Views: 388

Answers (2)

What compiler/toolchain/debugger are you using? If you are using GCC you can use the catch throw and catch catch commands to add breakpoints on exception throwing/catching.

As of the possible reasons that it is not being caught, if the only code in the catch block is a throw, the compiler might have optimized the whole block away. Consider adding any instruction to the block (note any might require some instruction with actual side effects, or the compiler might also optimize that away)

Upvotes: 0

wilx
wilx

Reputation: 18228

From the code you show you

  1. throw an exception;
  2. catch it;
  3. rethrow it;
  4. never catch it again.

Item 4 seems to be the important part.

Upvotes: 2

Related Questions