Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any exception that catch(...) can handle while catch(exception& ex) cannot?

Tags:

c++

c++11

I have searched a lot for the differences between these two types of handlers, but everyone said that catch(...) is a generic handler which catches everything.

I could not find any exception that one of them can handle and the other cannot. Even divide by zero, creates an exception that both of them cannot handle (floating point exception).

Can anyone give me a sample and explain clearly their difference? Which one of them should I use?

like image 562
JGC Avatar asked Jul 12 '16 07:07

JGC


People also ask

Can we handle exception in catch block?

In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception. Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final .

Can we catch multiple exceptions in the catch block?

Handle Multiple Exceptions in a catch Block In Java SE 7 and later, we can now catch more than one type of exception in a single catch block. Each exception type that can be handled by the catch block is separated using a vertical bar or pipe | .

Does catch exception catch everything?

If you catch some Exception types and don't do anything with the information, you have no chance of knowing what went wrong in those situations, but if you catch all Exception subclasses you have no chance of knowing what went wrong in a much large number of situations.

How do you handle two exceptions in single catch?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block. An old, prior to Java 7 approach to handle multiple exceptions.


1 Answers

Is there any exception that catch(...) can handle while catch(exception& ex) cannot?

Yes, any exception that is not, or not derived from, std::exception will not be caught by catch(exception&). For example; throw 42; will be caught by catch(...), but not catch(exception&).

A throw exception(); can be caught by either; the first handler is chosen. The catch(...) should be the last handler.

Even divide by zero, creates an exception which both of them cannot handle (floating point exception)...

Visual Studio has two modes of exception handling; synchronous (/EHsc) and asynchronous (/EHa). For the synchronous handling, both catch(...) and catch(exception&) will only catch C++ exceptions; i.e. those thrown with a throw xyz;. For the asynchronous handling, access violations etc. can be caught with catch(...).

VS also offers a function _set_se_translator() that can be used to "translate" or handle the Win32 exceptions. Typically the function is used to translate the Win32 exception to a C++ exception (derived from exception) that can be handled by a catch(exception&).

A sample of what the translation could look like (some detail is omitted for brevity);

struct seh_exception : std::runtime_error {
    //...
};

struct access_violation : seh_exception {
    //...   
};

struct unspecified_seh_exception : seh_exception {
    //...
};

void translation_function(unsigned int code, ::EXCEPTION_POINTERS* info)
{
    switch (code) {
    case EXCEPTION_ACCESS_VIOLATION:
        throw access_violation();
        break;
    // more cases for other exception codes
    };

    throw unspecified_seh_exception();
}

Which one of them should I use?

You should use the one that you can do something with. If all exceptions result in the same handling by your code, then use catch(...). If your code needs to handle the exceptions separately, then you should have a handler for each exception that is expected.

like image 174
Niall Avatar answered Sep 28 '22 08:09

Niall