views:

113

answers:

8

Why would my try-catch block still be throwing an error when it's handled?

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

              Try
    Here >> :   _MemoryStream.Seek(6 * StartOffset, 0)
                _MemoryStream.Read(_Buffer, 0, 6)
              Catch ex As IOException
                // Handle Error
              End Try

Edit: Cleaned the question up to remove the extraneous information.

+6  A: 

the application is blowing up on a line inside of a try-catch block. Any idea why this would be happening? Shouldn't it just be failing silently?

Why would you think that an exception can't occur within a try/catch? The whole purpose of the try.catch block is to define how you intend exceptional situations to be handled. If there is no catch block corresponding to the type of exception thrown, the exception will propogate out until either some code catches it or until it is raised as unhandled.

It's, of course, possible to use Catch ex as Exception as a block to catch all exceptions and then swallow them, but this is rarely a good idea.

As far as NullReferenceException goes, you almost never want to catch them and handle them (almost never). They generally are an indication that there is a bug somewhere in the code where logic is not testing a reference for null before accessing methods or properties on it. In fact, it's likely that the _MemoryStream variable is itself the culprit - if it's null then invoking a call on it would raise that exact exception.

LBushkin
+6  A: 

Since the try/catch block is only catching an IOException, it won't trap a NullReferenceException.

This likely indicates some kind of logic error in the program beyond the code you posted. And for the record, with this kind of exception, the program should not fail silently -- it's likely a bug in the code or in the way the library is being used (although it probably should have been handled by the library), not a problem with the runtime environment.

Jon Seigel
A: 

Because the exception being raised is not IOException. You need a catch all exception.

Raj Kaimal
A: 

There's nothing to say that the catch is catching all exceptions; it might be restricted to a certain class of exception that does not cover this one. Even if it is catching this class of exception, it might re-throw it. When done properly (i.e. throw;), re-throw will show the exception as thrown at the line inside the catch that caused the exception in the first place.

Tom Cabanski
+1  A: 
  1. Try-Catches aren't generally about failing silently. Just to clear that up.

  2. Is _MemoryStream null (or Nothing) at that point in code? try-catch should practically never be used to handle a NullReferenceException.
    in this case it doesn't catch NullReferenceException because a NullReferenceException isn't an IOException.

Greg D
+1  A: 

This is happening because you're only catching exceptions of type System.IOException, and the exception being thrown is a System.NullReferenceException. To catch it, you'd need to do this:

Try
   _MemoryStream.Seek(6 * StartOffset, 0)
   _MemoryStream.Read(_Buffer, 0, 6)
Catch ex As IOException

Catch ex As NullReferenceException
   ' Exception would be caught and handled here.
End Try
rwmnau
+1  A: 

I am no vb.net expert, but a null reference exception is not an IOException, so the exception falls through and must not be caught at a higher level. If you catch the IOException as Exception, that should do the trick, although not a best practice.

Rob Goodwin
Not only is it not a best-practice, it's practically always a bug.
Greg D
A: 

The Catch is catching an IOException (only). The exception being thrown is a NullReferenceException.

Wonko the Sane
Wow. 7 other answers in the time it took me to type my answer. :)
Wonko the Sane