?
Solved

Nested Exception Checking

Posted on 2009-12-18
2
Medium Priority
?
208 Views
Last Modified: 2013-11-25
If an error occurs in a called procedure, can that exception be passed back?

I created a test project to see if I return the except if that will be caught in the CallOne Try statement but it doesn't.

I guess I could put the CallOne() inside of an if statement.  Any suggestions?

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      CallOne()
      End
   End Sub

   Private Sub CallOne()
      Try
         CallTwo()

         'Rest of code which shouldn't execute if CallTwo has an exception.
         MessageBox.Show("Merry Christmas")
      Catch ex As Exception
         MessageBox.Show("1:" & ex.Message)
      End Try
   End Sub

   Private Function CallTwo() As Exception
      Dim ex As Exception = Nothing

      Try
         Throw New Exception("Test Error")
      Catch ex
         MessageBox.Show("2:" & ex.Message)
      End Try

      Return ex
   End Function
End Class

Open in new window

0
Comment
Question by:BC_Easyrider
2 Comments
 
LVL 30

Accepted Solution

by:
Reza Rad earned 2000 total points
ID: 26083688
this code will show first messagebox from sub CallTwo then second as Merry christmas,
if you want to go to catch block of sub CallOne when you hit an exception in callTwo , you must throw an exception in sub CallTwo and catch block like below, or don't use it in try-catch in sub CallTwo.

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      CallOne()
      End
   End Sub

   Private Sub CallOne()
      Try
         CallTwo()

         'Rest of code which shouldn't execute if CallTwo has an exception.
         MessageBox.Show("Merry Christmas")
      Catch ex As Exception
         MessageBox.Show("1:" & ex.Message)
      End Try
   End Sub

   Private Function CallTwo() As Exception
      Dim ex As Exception = Nothing

      Try
         Throw New Exception("Test Error")
      Catch ex
         Throw New Exception(ex.Message)
      End Try

      Return ex
   End Function
End Class


0
 

Author Closing Comment

by:BC_Easyrider
ID: 31667930
This makes complete sense.  I'm not sure if what I'm trying to do it good coding practice but I'm trying to simplify my code when an error occurs.  There's no sense sometimes for the calling procedure to continue on if there's an error.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 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