简体   繁体   中英

Exception causing a leap in code

I am using D5 and Synaps with OpenSSL and it is working great. I am trying to handle crashes so have deliberately entered bad data, one try at a time. So far I have handled the induced crashes OK, but I just changed the login Password and tried to login.

gMajorFail:=False;
if not pop3.Login() then
begin
  gMajorFail:=True;
  raise EPOP3.Create('POP3 ERROR: '+IntToStr(pop3.Sock.LastError)+
                     ' When trying to Login to Account');
end;
if gMajorFail then GoTo HadFailure;

Instead of jumping to the HadFailure-Label, it jumps to the last line of code in the procedure.

I have tried using Try/Finally (that's why I am using the Label to GoTo) but it still skips right to the last line of code.

Where am I going wrong and how can I fix it?

Thanks

oh boy... kill that GoTo with fire.

That said, when you raise an exception that's the end of the line, it does not return control to the remainder of your method - execution is immediately passed to the exception handler (ie: the nearest parent except/finally block is triggered or, if none exists, you get the "unhandled exception" dialog). When you're raising an exception you are essentially throwing up your arms and intend it to mean that your own code has no further error handling that can correct the issue and that there is nothing more your code needs to do. If you need to clean up or otherwise set some remaining things in order, do all of that first and then raise the exception as the last thing you do.

From the documentation :

When an exception is raised - that is, referenced in a raise statement - it is governed by special exception-handling logic. A raise statement never returns control in the normal way. Instead, it transfers control to the innermost exception handler that can handle exceptions of the given class. (The innermost handler is the one whose try...except block was most recently entered but has not yet exited.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM