Question

Given code similar to this:

Try
    Try
        'Code that throws an OleDBException
    Catch dbEx as System.Data.OleDb.OleDbException
        Console.WriteLine("Inner Specific Exception handler.")
        Dim newEx As New System.Exception("Database Error Handled.", dbEx)
        Throw newEx
    Catch ex as Exception
        Console.WriteLine("Inner Generic Exception handler.")
    End Try
Catch ex as Exception
    Console.WriteLine("Outer Generic Exception handler.")
End Try

This code has been slimmed down a lot and the actual implementation does more with the OleDBException then lose the stack trace but my question is, what will be written to the console when a OleDbException is thrown?

I'm hoping to see:

Inner Specific Exception handler.
Inner Generic Exception handler.

But I think it will actually output:

Inner Specific Exception handler.
Outer Generic Exception handler.

Or does anyone know of a sure fire way to cause an OleDbException to be thrown consistently so I could test this myself?

Was it helpful?

Solution

You can't easily throw an OleDBException because it says cannot access friend constructor, but why not test it out with a simpler type of exception? The type of exception won't make a difference for your test.

  Try
     Try
        Throw New ArgumentException("Throwing up!")
        'Code that throws an OleDBException
     Catch dbEx As ArgumentException
        Console.WriteLine("Inner Specific Exception handler.")
        Dim newEx As New System.Exception("Database Error Handled.", dbEx)
        Throw newEx
     Catch ex As Exception
        Console.WriteLine("Inner Generic Exception handler.")
     End Try
  Catch ex As Exception
     Console.WriteLine("Outer Generic Exception handler.")
  End Try

OTHER TIPS

It appears that the constructor for the OleDbException class is not public, so you may not be able to simply throw one yourself.

You can accomplish your goal, though, by executing a query via the OleDB connection that you know will fail. For example:

SELECT NoColumn FROM NoTable

Should do the trick, unless you're unlucky enough to have a table called "NoTable" with a column called "NoColumn". :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top