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!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should use a
try/catchblock.As others have already answered,
__try/__exceptis for catching SEH (windows generated errors) not for catching general exceptions.Most importantly,
__tryand__catchmay 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
/EHaare 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
tryandcatchsimply because they are standard, while__tryand__exceptare not.