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.
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 Exceptionas a block to catch all exceptions and then swallow them, but this is rarely a good idea.As far as
NullReferenceExceptiongoes, 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 fornullbefore accessing methods or properties on it. In fact, it’s likely that the_MemoryStreamvariable is itself the culprit – if it’s null then invoking a call on it would raise that exact exception.