How can I catch an inner exception in .NET? I need to check 2 databases for a record. The database code throws an exception if a record is not found, so then 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 pseudo-code fails at 'Fails here and ex2 never is handled by my code.
How should I properly handle the inner exception?
The reason your current code doesn’t work is that once you enter the catch section, you’ve already left the try block. Instead, do it like this:
Or like this: