Jump to content

Try-Except/Finally?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Would someone explain Try-Except to me? Microsoft is being confusing. I understand the basic idea (I understand Try-Catch), but I don't really get what the expression in __except([expression]) is for. Also, what's __finally for, and when is it executed? Thank you!

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
As far as I can read, __Try-__Except works precisely the same way as "Try-Catch" does, it's just that Microsoft added that extension for use in the C language. If you're going to be programming in C++, just use the native Try-Catch system of exception handling, since that will work on more than just Windows and is supported by all compilers.

__finally is the same as Finally in C++ exception handling as well, it's executed at the end of the try block whether or not an exception was thrown. It's usually used to handle dynamically allocated memory.
Wow I changed my sig!

#3
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Okay, that makes sense.
The reason I'm using __try __except is because I'm hoping to catch access violations.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Have you tried this:
try {
  // Statement that throws access violation
  } catch (...) {
    std::cout << "Access violation thrown!" << std::endl;
  }
Just checking?
Wow I changed my sig!

#5
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
yup.

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Does the access violating code return some kind of signal that you know of at all?

EDIT: And, could you give me a small example of how this is being used, so that I can help troubleshoot it? I understand proprietary and stuff, but just a small example of the input you receive, how you give the input to the assembly call, then what variable the output is applied to should be sufficient.
Wow I changed my sig!

#7
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
Depends what you mean by "signal". Access violations are caught by debuggers, but other than that, no checks/handling is performed by the function.

__try __except works perfectly. Thanks!