Link to home
Start Free TrialLog in
Avatar of prosit
prositFlag for United States of America

asked on

Passing try-catch result back to calling form

Hello,

How do I pass the exception from a function in a class back to the calling function on a form?

Dumb Example:

Sub CallingCode

    CallSomeFunction

End Sub

Sub CallSomeFunction
    try
        result = 10/0
    catch
        ' return this exception back to the callingcode function
    end try

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of prosit
prosit
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would be surprised if prosit solution worked, because ex has not been defined.

An even if it was, rethrowing a catched exception removes the stacktrace, one of the best piece of information you can have in an exception.

The right way to do it is even simpler, and it keeps all the information intact:

catch
    throw
Avatar of prosit

ASKER

Hey,

It does work when defining ex but I appreciate you taking the time to show a better way so thank you.

J
To define ex, you would do it the following way:

  try
         result = 10/0
     catch ex As Exception
         throw ex
     end try

This is what you see everywhere, so it is repeated everywhere else. But what I showed you is better because you keep the StackTrace.
Avatar of prosit

ASKER

Yup, VS does that part for you, I typed it in the editor so I forgot that :)

Thanks again and merry Christmas (or whichever you prefer )