Solved

Passing try-catch result back to calling form

Posted on 2014-12-05
5
79 Views
Last Modified: 2014-12-06
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

0
Comment
Question by:prosit
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
prosit earned 0 total points
ID: 40482867
ugh so simple...

Sub CallSomeFunction
    try
        result = 10/0
    catch
        throw ex
    end try

End Sub
0
 
LVL 40
ID: 40484729
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
0
 
LVL 2

Author Comment

by:prosit
ID: 40484748
Hey,

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

J
0
 
LVL 40
ID: 40484866
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.
0
 
LVL 2

Author Comment

by:prosit
ID: 40484909
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 )
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question