How do I catch an inner exception in .NET?

How can I catch an inner exception in .NET? I need to check 2 databases for writing. The database code throws an exception if no record is found, so I want to check the second database:

Try
     # Code to look in database 1
Catch ex as DataServiceQueryException
     Try
          # Code to look in database 2
     Catch ex2 as DataServiceQueryException
          throw New DataServiceQueryException(ex.Message, ex2) # Fails here
     End Try
Catch ex as Exception # Why doesn't ex2 land here?
   # Tell user that data was not found in either database
End Try

      

The above pseudocode doesn't work in 'Fails here

, and ex2 is never handled by my code.

How to properly handle the inner exception?

+2


source to share


5 answers


The reason your current code is not working is because as soon as you enter the catch section, you have already left the try block. Instead, do the following:

Try
   ''# Check Database 1
Catch
    Try
        ''# Check Database 2
    Catch
         ''# Tell the user that data was not found in either database
    End Try
End Try

      



Or like this:

Dim FoundFlag as Boolean = False
Try
    ''# Check Database 1
    FoundFlag = True
    ''# Best if you can just return "False" and avoid the exception altogether
Catch
End Try

If Not FoundFlag Then
    Try
        ''# Check Database 2
        FoundFlag = True
    Catch
    End Try
End If

If Not FoundFlag Then
     ''# Tell the user that data was not found in any database
End If

      

+10


source


The inner exception is by definition already handled and repackaged as another exception. You should handle the outer exception and then, if necessary / appropriate process, inner in the catch block of the outer exception.



+3


source


First, if you are using try / catch, you should probably finally clean up resources. That being said, if nested try / catch blocks are blocking us, usually the code smell. Should you implement it this way? Why does the server just fail? Why was the Data Layer unable to transmit the status message? Exceptions must be, well, "exceptional".

If you need to use exceptions, "Joel Coehoorn" seems like a good thing.

+1


source


I would say, based on pseudocode, that this is because the exception you are throwing on line 7 is inside a "try" block around line 3, so the "catch" on line 9 just doesn't apply.

Edit: what Joel said.

0


source


I agree with Joel, and I would further encourage you to sit down and decide which of the following cases you really want, and then code accordingly.

Case A. If entry11 exists in db1 then check if entry22 exists in db2

try{
  getRecord11;

  try
  {
    getRecord22;
  }
  catch ex22
  {
    saySorry2;
  }
}
catch ex11
{
  saySorry1;
}

      

case B. If record11 does not exist in db1 then check if record22 exists in db2

try{
  getRecord11;
}
catch ex11
{
  saySorry1;

  try
  {
    getRecord22;
  }
  catch ex22
  {
    saySorry2;
  }
}

      

case C. Get record11 from db1. Regardless of db1 results, get record22 from db2.

try{
  getRecord11;
}
catch ex11
{
  saySorry1;
}

try
{
  getRecord22;
}
catch ex22
{
  saySorry2;
}

      

0


source







All Articles