When is an InvalidCastException not an InvalidCastException?

by Sander Gerz February 19, 2004 16:56

Strange things occur sometimes. Like this error message I get when running my project under development:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:
System.InvalidCastException: Cast from type 'DBNull' to type 'Integer' is not valid.

Source Error:


Line 1348:            Get
Line 1349:                Try 
Line 1350:                    Return CType(Me(Me.tableCP_P_WEBMODULES.WEBMODULE_IDColumn),Integer)
Line 1351:                Catch e As InvalidCastException
Line 1352:                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)

So the faulty code is in enclosed in a Try-Catch statement. The Catch-block catches an InvalidCastException. However, when that actual type of exception is thrown, it's not handled.

Surely, I can make sure that this code never throws an exception by checking the datatype before calling the method. But that doesn't explain why the exception is not caught right here. Ideas anyone?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen | Modified by Mooglegiant