Justin R.
Justin R.

Reputation: 24071

Handling almost all exceptions in C++

I have a try block which currently catches all exceptions:

try
{
    // do some work
}
catch (std::exception &ex)
{
    // log ex
}

However, I do not want to catch access violations. Can I specify that as an exception (so to speak) of my handler? Or should I catch it first and rethrow it?

Upvotes: 0

Views: 116

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

You are already not catching access violations and you never could. Access violations are not C++ exceptions. They are "exceptions" of a different kind — that raised by your operating system. I prefer not to call them "exceptions" at all, in fact.

Linux and Linux-like operating systems simply terminate a process (using a signal) that performs an access violation.

Windows instead uses something called "structured exceptions" which you can potentially catch and possibly ignore using language extensions in Visual Studio. We're venturing off-topic now, but you could read up about those. I still wouldn't recommend their use, mind you. Once you have an access violation I'd personally be content to say "all bets are off", and "we have some debugging to do".

Upvotes: 10

Related Questions