Home  >  Article  >  Backend Development  >  How Does Structured Exception Handling (SEH) Work in C with __try and try/catch/finally?

How Does Structured Exception Handling (SEH) Work in C with __try and try/catch/finally?

Barbara Streisand
Barbara StreisandOriginal
2024-11-07 13:11:03318browse

How Does Structured Exception Handling (SEH) Work in C   with __try and try/catch/finally?

Structured Exception Handling (SEH) in C with __try and try/catch/finally

In C , the try/catch/finally blocks provide a mechanism for handling exceptions within a code block. However, on Windows, the compiler also supports a different approach called Structured Exception Handling (SEH).

The __try keyword is a non-standard keyword that allows you to catch SEH exceptions. SEH exceptions are supported at the operating system level, providing a way to catch exceptions from the operating system itself or from code that uses SEH for exception handling.

The __except keyword lets you specify an exception filter expression that determines whether or not an active exception should be caught. This allows you to selectively handle certain types of exceptions.

The __finally keyword lets you add code that runs after the exception has been handled. This can be useful for performing cleanup actions, such as freeing resources or closing files.

SEH exceptions are implemented on top of C exceptions. C exceptions are thrown using the throw keyword and can be caught using the catch keyword. When a C exception is thrown, the compiler creates a corresponding SEH exception that can be caught by __try/__except blocks.

The EHa compile option forces the compiler to always generate code to register the destructors of objects that are created in the scope of a try block. This ensures that destructors are called even in the presence of asynchronous SEH exceptions.

To demonstrate how SEH exceptions can be caught in C , consider the following program:

class Example {
public:
    ~Example() { std::cout << "destructed" << std::endl; }
};

int filterException(int code, PEXCEPTION_POINTERS ex) {
    std::cout << "Filtering " << std::hex << code << std::endl;
    return EXCEPTION_EXECUTE_HANDLER;
}

void testProcessorFault() {
    Example e;
    int* p = 0;
    *p = 42;
}

void testCppException() {
    Example e;
    throw 42;
}

int main()
{
    __try {
        testProcessorFault();
    }
    __except(filterException(GetExceptionCode(), GetExceptionInformation())) {
        std::cout << "caught" << std::endl;
    }
    __try {
        testCppException();
    }
    __except(filterException(GetExceptionCode(), GetExceptionInformation())) {
        std::cout << "caught" << std::endl;
    }
    return 0;
}

When you run this program with the EHa compile option set, the output will show that the destructors of the Example objects are called, even when SEH exceptions are raised. This demonstrates how SEH exceptions and C exceptions can be handled together in C code.

The above is the detailed content of How Does Structured Exception Handling (SEH) Work in C with __try and try/catch/finally?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn