Appleoddity
Appleoddity

Reputation: 1181

How to pass exception to caller

This seems like is has to be simple, but I'm having trouble finding a simple straight forward answer.

Consider the following:

Sub Caller()
  Try
    Callee()
  Catch ex As Exception
    MsgBox(Ex.Message)
  End Try
End Sub

Sub Callee()
  Try
    'Generate an error
  Catch ex As Exception
    'Pass the exception back to Caller.
    'I tried using 'throw' but it errored out inside this function
  End Try
End Sub

How do I not handle the exception inside Callee and instead pass the exception back to Caller?

This is a highly simplified view of some code that actually uses functions that return data already. So, I can't use a function with a return value to pass back the exception.

Thanks!

Upvotes: 0

Views: 1658

Answers (1)

Tom F
Tom F

Reputation: 817

If you only want Caller to handle the exception, then remove the Try-Catch block from the Callee method. Any exception thrown by the Callee method will automatically be caught within the Try-Catch block inside the Caller method.

Upvotes: 2

Related Questions