Avatar of Thomasian
Thomasian
Flag for Philippines asked on

Throwing Exception VB.NET

If an exception is unhandled inside a class, will the exception be automatically passed to the calling application?
.NET ProgrammingEditors IDEsVisual Basic.NET

Avatar of undefined
Last Comment
Thomasian

8/22/2022 - Mon
Fernando Soto

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
   
Thomasian

ASKER
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 Soto

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Thomasian

ASKER
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 Soto

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.
Thomasian

ASKER
But another exception is thrown inside the Catch block which is unhandled. Shouldn't it exit the function?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Thomasian

ASKER
Thanks again.
Éric Moreau

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

ASKER
Thanks Eric. That is very helpful! :-)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes