Solved

Passing try-catch result back to calling form

Posted on 2014-12-05
5
81 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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