Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:58:57+00:00 2026-06-12T04:58:57+00:00

Here’s the code example : Try Throw New FirstException() Finally Throw New SecondException() End

  • 0

Here’s the code example :

Try
    Throw New FirstException()
Finally
    Throw New SecondException()
End Try

I figured out it only throws SecondException out and FirstException just vanishes.

I thought FirstException would be inside InnerException property of SecondException but it appears it is not.

I’m not blocked on anything as I don’t really need the FirstException to show up, I’m just rather intrigued about this behaviour.

  • Is there a way to know SecondException did get thrown first when
    catching it all at upper level ?

  • If the first exception really is overriden by the second, what is the
    reason ?

  • Does it happen in every other language ? Is it logical ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:58:58+00:00Added an answer on June 12, 2026 at 4:58 am

    One of the limitations of exception handling in .net is that there is no nice way for code in a Finally block to know what exception, if any, caused the code in the Try block to exit, nor is there any normal way for code in a finally block which does have such information to make it available to code which might throw an exception.

    In vb.net, it’s possible to kludge things in a manner that works pretty well, even though it looks a bit ugly.

    Module ExceptionDemo
        Function CopySecondArgToFirstAndReturnFalse(Of T)(ByRef dest As T, src As T) As Boolean
            dest = src
            Return False
        End Function
        Function AnnotateExceptionAndReturnFalse(ex As Exception, TryBlockException As Exception) As Boolean
            If ex Is Nothing Then Return False ' Should never occur
            If TryBlockException Is Nothing Then Return False ' No annotation is required
            ex.Data("TryBlockException") = TryBlockException
            Return False
        End Function
    
        Sub ExceptionTest(MainAction As Action, CleanupAction As Action)
            Dim TryBlockException As Exception = Nothing
            Try
                MainAction()
            Catch ex As Exception When CopySecondArgToFirstAndReturnFalse(TryBlockException, ex)
                ' This block never executes, but above grabs a ref to any exception that occurs
            Finally
                Try
                    CleanupAction()
                Catch ex As Exception When AnnotateExceptionAndReturnFalse(ex, TryBlockException)
                    ' This block never executes, but above performs necessary annotations
                End Try
            End Try
        End Sub
    
        Sub ExceptionTest2(Message As String, MainAction As Action, CleanupAction As Action)
            Debug.Print("Exception test: {0}", Message)
            Try
                ExceptionTest(MainAction, CleanupAction)
            Catch ex As Exception
                Dim TryBlockException As Exception = Nothing
                Debug.Print("Exception occurred:{0}", ex.ToString)
                If ex.Data.Contains("TryBlockException") Then TryBlockException = TryCast(ex.Data("TryBlockException"), Exception)
                If TryBlockException IsNot Nothing Then Debug.Print("TryBlockException was:{0}", TryBlockException.ToString)
            End Try
            Debug.Print("End test: {0}", Message)
        End Sub
        Sub ExceptionDemo()
            Dim SuccessfulAction As Action = Sub()
                                                 Debug.Print("Successful action")
                                             End Sub
            Dim SuccessfulCleanup As Action = Sub()
                                                  Debug.Print("Cleanup is successful")
                                              End Sub
            Dim ThrowingAction As Action = Sub()
                                               Debug.Print("Throwing in action")
                                               Throw New InvalidOperationException("Can't make two plus two equal seven")
                                           End Sub
            Dim ThrowingCleanup As Action = Sub()
                                                Debug.Print("Throwing in cleanup")
                                                Throw New ArgumentException("That's not an argument--that's just contradiction")
                                            End Sub
            ExceptionTest2("Non-exception case", SuccessfulAction, SuccessfulCleanup)
            ExceptionTest2("Exception in main; none in cleanup", ThrowingAction, SuccessfulCleanup)
            ExceptionTest2("Exception in cleanup only", SuccessfulAction, ThrowingCleanup)
            ExceptionTest2("Exception in main and cleanup", ThrowingAction, ThrowingCleanup)
        End Sub
    End Module
    

    The module above starts with a couple helper modules which should probably be in their own “Exception helpers” module. The ExceptionTest method shows the pattern for code which might throw an exception in both the Try and Finally block. The ExceptionTest2 method calls ExceptionTest and reports what exception if any comes back from it. ExceptionDemo calls ExceptionTest2 in such a way as to cause exceptions in different combinations of the Try and Finally blocks.

    As shown, if an exception occurs during cleanup, that exception will be returned to the caller, with the original exception being an item in its Data dictionary. An alternative pattern would be to catch the exception that occurs on cleanup and include it in the data of the original exception (which would be left uncaught). My general inclination is that it’s probably better in many cases to propagate the exception that occurs during cleanup, since any code which was planning to deal with the original exception will probably expect that cleanup succeeded; if such an expectation cannot be met, the exception that escapes should probably not be the one the caller was expecting. Note also that the latter approach would require a slightly different method of adding information to the original exception, since an exception which is thrown in a nested Try block might need to hold information about multiple exceptions that were thrown in nested Finally blocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is an example: I write html code inside of textarea, then I swap
Here is the code I'm using inside my AsyncTask DefaultHttpClient httpClient = new DefaultHttpClient();
Here's the code require_once 'functions.php'; require_once 'cfg.php'; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
Here's the code I have. It works. The only problem is that the first
here is my code, SiteMember class @OneToMany(mappedBy = member,cascade=CascadeType.ALL) private List<MemberThread> memberThread = new
here are 2 screen shots when i try to debug my code in visual
Here a simple question : What do you think of code which use try
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.