Hi,
I'm wondering which is the better way to catch exceptions that I throw: is it a __try / __except block or a try / catch block?
I'm writing in C++ and the program will only be used on Windows, so portability is not an issue.
Thanks!
Hi,
I'm wondering which is the better way to catch exceptions that I throw: is it a __try / __except block or a try / catch block?
I'm writing in C++ and the program will only be used on Windows, so portability is not an issue.
Thanks!
They are two very different things. try/catch are the familiar C++ keywords you know. __try/__except
is used to catch SEH exceptions. Exceptions raised by Windows itself, like DivisionByZero or AccessViolation. It is well described in the MSDN Library article for it.
You can also use it to catch C++ exception because it leverages the Windows SEH feature. You however can't get the thrown exception object out of it so there will be zero context if you actually want the handle the exception. Which is madness. The number one approach is to not ever catch SEH exceptions, they are always gross. If you do need to marry the two then use _set_se_translator() to convert the SEH exception to a C++ exception.
__try/__except
is designed for calling Win32 C code which doesn't support exceptions but does use a structured error code / handling mechanism. __try/__except
will translate C errors into an exception block similar to C++ try/catch.
For more information, see this MSDN article.
Standard C++ uses try/catch blocks, so I would recommend using them, if you need the "standard" exception mechanism, based on standard C++ library.
However, if you plan to use Structured Exception Handling provided through Windows SDK (see here), then use __try
/__except
.
You should use a try
/catch
block.
As others have already answered, __try
/ __except
is for catching SEH (windows generated errors) not for catching general exceptions.
Most importantly, __try
and __catch
does not run C++ destructors or correctly unwind the stack when an exception is thrown.
Except in rare cases, you should never try to catch SEH exceptions.
EDIT: Well, I was positive on this (it's what I've always been told), but @Hans says that apparently there is a compiler switch you can use to change this. I think the docs on /EHa
are misleading, or at least incomplete, on what happens here. If someone finds definitive docs which prove this wrong, I'll happily remove this answer.
Even if it turns out this is false, you should still use try
and catch
simply because they are standard, while __try
and __except
are not.
Once you've thrown something, you no longer have much choice about how to catch it. If you throw C++ exceptions (i.e., with throw
), then use try
/catch
. If you throw Windows exceptions (i.e., with RaiseException
), then use __try
/__except
. Trying to mix them will just be adding unnecessary hassle to your life.