George Johnston
George Johnston

Reputation: 32278

Why would an error get thrown inside my try-catch?

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.

Upvotes: 1

Views: 452

Answers (8)

Jon Seigel
Jon Seigel

Reputation: 12401

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.

Upvotes: 6

Wonko the Sane
Wonko the Sane

Reputation: 10823

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

Upvotes: 0

Rob Goodwin
Rob Goodwin

Reputation: 2777

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.

Upvotes: 1

SqlRyan
SqlRyan

Reputation: 33924

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

Upvotes: 1

Greg D
Greg D

Reputation: 44096

  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.

Upvotes: 1

Tom Cabanski
Tom Cabanski

Reputation: 8018

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.

Upvotes: 0

Raj Kaimal
Raj Kaimal

Reputation: 8304

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

Upvotes: 0

LBushkin
LBushkin

Reputation: 131796

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.

Upvotes: 6

Related Questions