Errors in ASP.NET

  • Thread starter Thread starter DaMan
  • Start date Start date
D

DaMan

I have a ASP.NET page with a TRY / CATCH in it
essentially the code reads

TRY
do code
CATCH ex as EXCEPTION
label1.TEXT = ex.MESSAGE
myresult = ex.HRESULT <<< error!
END TRY
I need to find the exact error number stored in EXCEPTION.HRESULT, however
it is not an available field in ex.XXXX
ex.HRESULTS says it is protected but ex.MESSAGE is OK, how do I get the
HRESULT.
Thanks...
 
You don't.

In .NET, everything is an object, or better yet a type. In the VB 6.0 days
we got error numbers so we would know what "type" of error was returned. In
..NET, we don't need a number to tell us that, we just need to know its
"type".

Take a simple "answer = number1 / number2" for example:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim x, y as Short
Try
x = txtBox1.text
y = txtBox2.text
txtBox3.text = x / y
Catch ex As System.OverflowException
txtErrMessage.text = "Your number was out of the acceptable range"
Catch ex As System.InvalidCastException
txtErrMessage.text = "Please enter numeric data only."
Catch ex As Exception
txtErrMessage.text = "Error of type: " & ex.GetType.ToString
End Try
End Sub

With "Option Strict" turned on, we will get an InvalidCastException when we
attempt to make x = txtBox1.text, the first Catch statement will be hit.

If we enter extremely large or small values, we will get an
OverflowException and for anthing that we couldn't predict, the generic
"Catch" will be hit and we will display what kind of exception we did get in
that case.
 
Basically, you don't. A Protected field or property is only accessible
within classes that inherit a given class. This means that in order to
access the HRESULT you would have to create an Exception class of your own
that inherits System.Exception, and that class could expose the HRESULT if
necessary. However, unless you were writing your own app and throwing that
specific exception, it would never show up, as the Exception types that are
thrown by the built-in .Net classes don't expose it.

There is a reason why the HRESULT is protected. The Exception Type indicates
what type of Exception was thrown, and Reflection can reveal this. The
Message indicates the details of the Exception. The number has never been
much help to anyone.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top