Exception processing seems odd


(mtiede) #1

I have this code I’m starting to work on:

   function VSDSService.ValidateLogin: VSDSLoginToken;
      begin
      result := new VSDSLoginToken;
      try
         var username := WebOperationContext.Current.IncomingRequest.Headers["username"];
         var password := WebOperationContext.Current.IncomingRequest.Headers["password"];

         result.Token := 'got a token';

         raise new WebFaultException<String>(String.Format("Authentication exception", "User not authenticated"), System.Net.HttpStatusCode.Unauthorized);

      except
         on E: WebFaultException do begin
            result.Error                := new VSDSLoginError;
            result.Error.Message        := E.Message;
            result.Error.StackTrace     := E.StackTrace;
            result.Error.InnerException := E.InnerException;

            EventLog.WriteEntry( 'VsdsRest',
               'Error in ValidateLogin at '     + DateTime.Now.ToString     + Environment.NewLine + Environment.NewLine +
               'Message: '                      + E.Message                 + Environment.NewLine + Environment.NewLine +
               'Stack Trace: '                  + E.StackTrace              + Environment.NewLine + Environment.NewLine +
               'InnerException: '               + E.InnerException:Message,
               EventLogEntryType.Error,  
               65535 );
            raise;
            exit;
            end;
         on E: Exception do begin
            result.Error                := new VSDSLoginError;
            result.Error.Message        := E.Message;
            result.Error.StackTrace     := E.StackTrace;
            result.Error.InnerException := E.InnerException;

            EventLog.WriteEntry( 'VsdsRest',
               'Error in ValidateLogin at '     + DateTime.Now.ToString     + Environment.NewLine + Environment.NewLine +
               'Message: '                      + E.Message                 + Environment.NewLine + Environment.NewLine +
               'Stack Trace: '                  + E.StackTrace              + Environment.NewLine + Environment.NewLine +
               'InnerException: '               + E.InnerException:Message,
               EventLogEntryType.Error,  
               65535 );
            end;
         end;

      end;

I expected the first On block to be entered and the result would be filled in and then the exception would be reraised. But the debugger shows it going directly to the exit statement. And the client does not see a 401 from the raise trying to reraise the exception.

Oxygene versions is 8.3.95.2031


(Patrick Lanz) #2

Mark,
You’re raising WebFaultException<T> exception and catch WebFaultException.
If you’re looking at the documentation, WebFaultException<T> doesn’t inherit from WebFaultException, that’s why the exception is not catched.


(mtiede) #3

It seems a little odd that it hits the EXIT statement inside the block that it shouldn’t be executing, according to you. But I suspect you are right and the exit statement executing is just some compiler artifact. I’ll try adding the . I expected that ANY WebFaultException would get caught when no type was specified, but I guess that was a bad assumption.

Thanks.

… time passes …

Yep, adding the < String > allowed it to do what I expected. Thanks again.


(Carlo Kok) #4

Ah sorry about that. “exit” isn’t really where it’s running, but there’s a positionless jump after the exit that triggers this.


(Patrick Lanz) #5

Writing

  raise;
  exit;

in an exception hadler makes no sense: after raise, the exception is raised again and the following code is not executed.


(mtiede) #6

Patrick,

Yep, I know. As I said, I was just starting to work on this code. I just threw the raise in there temporarily to see how that would get back to my client.

But you are right.

That was why I was surprised when the debugger STOPPED on that exit statement.