We help IT Professionals succeed at work.

Throwing Exception VB.NET

1,696 Views
Last Modified: 2013-11-26
If an exception is unhandled inside a class, will the exception be automatically passed to the calling application?
Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi Thomasian;

To your statement, " If an exception is unhandled inside a class, will the exception be automatically passed to the calling application?" Let me state it this way if an exception is thrown in a class and it is not handled it will bubble up to the caller and will continue to bubble up the call stack until one of two things happen It is caught by your code or the CLR will catch it and display a Unhandled exception error message to the user and terminate the program.

Fernando
   

Author

Commented:
Fernando,

Does that mean that I don't need to catch exceptions that I'm just going to throw again?

I have attached a sample code below (from Mastering VB 2008).

Does that mean that I can just leave out the Try...Catch... section since it will throw the exception anyway?
Public Overrides Function Equals(ByVal obj As Object) As Boolean
    Dim O As New Minimal()
    Try
        O = DirectCast(obj, Minimal)
    Catch typeExc As InvalidCastException
        Throw typeExc
        Exit Function
    End Try
    If O.BDate = m BDate Then
        Equals = True
    Else
        Equals = False
    End If
End Function

Open in new window

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Yes; if the only thing you will do is to re-throw the exception then do not place the code in a try/catch block and let the exception bubble up for the caller to catch. But remember that if you do not catch it in your code it will bubble up to the CLR and throw an Unhandled exception and terminate the program.

Author

Commented:
Does this also apply if the class is on a separate project? If an exception occurs, does it automatically exit the function? If it does, what is the purpose of "Exit Function"?
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
To your statement, "Does this also apply if the class is on a separate project?", Let say you have project A which is a class library/DLL and you have project B which is a EXE file and project B loads the DLL into it then any exceptions thrown by the project A will go through project B.

To your statement, "if an exception occurs, does it automatically exit the function?", The function is exited at the point of the exception and returns to the calling function.

To your statement, "If it does, what is the purpose of "Exit Function"?" When an exception is caught in a catch block as in the example given then the system will not terminate the function and continue processing after try block or if you had a finally block then it would continue in that block then after the End Try statement. When an exception is caught it is caught and not exception will be bubbled up the call stack.

Author

Commented:
But another exception is thrown inside the Catch block which is unhandled. Shouldn't it exit the function?
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks again.
√Čric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
I know that the question is closed but have a look at http://www.emoreau.com/Entries/Articles/2008/07/Exceptions.aspx

Author

Commented:
Thanks Eric. That is very helpful! :-)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.