473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to get most info from exception

Hi guys,

For my VB.NET application, I created an ErrorHandler class that should save
all available information about the last error occurred. I use
Server.GetLastError for this.
The problem is that I quite often get errors that I really can't say why
they happen because of the lack of information from GetLastError. This is an
example:

System.NullReferenceException: Object reference not set to an instance of an
object.
at MyApp.GlobalApp.Application_BeginRequest(Object sender, EventArgs e)
at
System.Web.SyncEventExecutionStep.System.Web.HttpA pplication+IExecutionStep.
Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean&
completedSynchronously)

Is this really all I can get from the exception? How can I tell what really
happened since our test group hasn't found any bugs when testing the site?

Thanks.

Best regards,
Jonah Olsson
Nov 18 '05 #1
8 1373
Instead of Server.GetLastError (which was the Classic ASP way of finding and
dealing with errors), use Structured Exception Handling which traps the
execption in line with your code and let's you handle different types of
exceptions.

The first step is to identify what line(s) of code are crashing the
application and then wrapping them in a "Try" statement. Then include a
"Catch" statement for each possible exception type. In each "Catch", you
can interrogate the exception using its .Message, .getType() and .source
members.

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.

"Jonah Olsson" <jo***@IHATESPAM.com> wrote in message
news:uQ****************@TK2MSFTNGP12.phx.gbl...
Hi guys,

For my VB.NET application, I created an ErrorHandler class that should save all available information about the last error occurred. I use
Server.GetLastError for this.
The problem is that I quite often get errors that I really can't say why
they happen because of the lack of information from GetLastError. This is an example:

System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.GlobalApp.Application_BeginRequest(Object sender, EventArgs e)
at
System.Web.SyncEventExecutionStep.System.Web.HttpA pplication+IExecutionStep. Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean&
completedSynchronously)

Is this really all I can get from the exception? How can I tell what really happened since our test group hasn't found any bugs when testing the site?

Thanks.

Best regards,
Jonah Olsson

Nov 18 '05 #2
Hi Scott,
and thanks for your reply!

I know of Try/Catch, but I thought there might be another (global) way of
tracking all errors within the application without using the Try/Catch
statement and still get detailed information.

How can I find/identify the line that are crashing the application without
being able to get more information from GetLastError? That is, I don't know
where to include the Try/Catch statement :-/

Thanks.
Jonah
"Scott M." <s-***@BADSPAMsnet.net> skrev i meddelandet
news:ec**************@tk2msftngp13.phx.gbl...
Instead of Server.GetLastError (which was the Classic ASP way of finding and dealing with errors), use Structured Exception Handling which traps the
execption in line with your code and let's you handle different types of
exceptions.

The first step is to identify what line(s) of code are crashing the
application and then wrapping them in a "Try" statement. Then include a
"Catch" statement for each possible exception type. In each "Catch", you
can interrogate the exception using its .Message, .getType() and .source
members.

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.

Nov 18 '05 #3
Determining where the application is crashing should be easy. Look at the
error page that comes up in the browser and it will tell you (in red) what
line raised the exception.

You can, of course, step through your code in the debugger and see what line
it dies on.

In my opinion, you shouldn't be using Server.GetLastError at all anymore
(although it is still supported). Structured exception handling is the
new/improved mechanism.
"Jonah Olsson" <jo***@IHATESPAM.com> wrote in message
news:OX**************@TK2MSFTNGP11.phx.gbl...
Hi Scott,
and thanks for your reply!

I know of Try/Catch, but I thought there might be another (global) way of
tracking all errors within the application without using the Try/Catch
statement and still get detailed information.

How can I find/identify the line that are crashing the application without
being able to get more information from GetLastError? That is, I don't know where to include the Try/Catch statement :-/

Thanks.
Jonah
"Scott M." <s-***@BADSPAMsnet.net> skrev i meddelandet
news:ec**************@tk2msftngp13.phx.gbl...
Instead of Server.GetLastError (which was the Classic ASP way of finding and
dealing with errors), use Structured Exception Handling which traps the
execption in line with your code and let's you handle different types of
exceptions.

The first step is to identify what line(s) of code are crashing the
application and then wrapping them in a "Try" statement. Then include a
"Catch" statement for each possible exception type. In each "Catch", you can interrogate the exception using its .Message, .getType() and .source
members.

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.


Nov 18 '05 #4
Yes, I do know that structured exception handling is the best/only way, but
in this case it seems like I haven't include any Try/Catch statement in this
part of the code.
As I wrote in my first post, non of our test group members has found this
problem, nor do I when I run debug or test it myself. This is just reported
every now and then through emails using a function with GetLastError called
from Application_BeginRequest.

I was just curious if I could get more information from GetLastError to help
me track down this kind of problem.

Jonah

"Scott M." <s-***@BADSPAMsnet.net> skrev i meddelandet
news:eh**************@TK2MSFTNGP11.phx.gbl...
Determining where the application is crashing should be easy. Look at the
error page that comes up in the browser and it will tell you (in red) what
line raised the exception.

You can, of course, step through your code in the debugger and see what line it dies on.

In my opinion, you shouldn't be using Server.GetLastError at all anymore
(although it is still supported). Structured exception handling is the
new/improved mechanism.

Nov 18 '05 #5
Ok Jonah, I got you now. Your first post didn't really say that the errors
were being reported by users of the application, rather than the developer
of the application.

Well, and I wouldn't recommend this as a normal bit of code, but you could
wrap each (or the suspected) procedure's code inside ONE Try statement and
have ONE catch that you can interrogate:

Sub Page_Load(sender as object, e as eventArgs)
Try
...All the code from the procedure goes here...
Catch ex as Exception
lblErrorMessage.Text = "Exception Type: " & ex.getType.ToString() &
"Exception Source: " & ex.source.ToString()
End Try
End Sub

You could do this for each procedure until you tracked the culprit down. I
know this seems like a lot of extra code, but in reality, you should really
be using Try...Catch wherever the possibility of exceptions (that are not
preventable) may exist.

"Jonah Olsson" <jo***@IHATESPAM.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Yes, I do know that structured exception handling is the best/only way, but in this case it seems like I haven't include any Try/Catch statement in this part of the code.
As I wrote in my first post, non of our test group members has found this
problem, nor do I when I run debug or test it myself. This is just reported every now and then through emails using a function with GetLastError called from Application_BeginRequest.

I was just curious if I could get more information from GetLastError to help me track down this kind of problem.

Jonah

"Scott M." <s-***@BADSPAMsnet.net> skrev i meddelandet
news:eh**************@TK2MSFTNGP11.phx.gbl...
Determining where the application is crashing should be easy. Look at the error page that comes up in the browser and it will tell you (in red) what line raised the exception.

You can, of course, step through your code in the debugger and see what

line
it dies on.

In my opinion, you shouldn't be using Server.GetLastError at all anymore
(although it is still supported). Structured exception handling is the
new/improved mechanism.


Nov 18 '05 #6
Thanks for you help! I really appreciate it.
And I'm sorry for not being clear enough in my first post.

I think I have to go through all my code then and insert Try/Catch
statements where those are missing.

Again, thanks for you help and time!

Regards,
Jonah Olsson
"Scott M." <s-***@BADSPAMsnet.net> skrev i meddelandet
news:eE**************@TK2MSFTNGP12.phx.gbl...
Ok Jonah, I got you now. Your first post didn't really say that the errors were being reported by users of the application, rather than the developer
of the application.

Well, and I wouldn't recommend this as a normal bit of code, but you could
wrap each (or the suspected) procedure's code inside ONE Try statement and
have ONE catch that you can interrogate:

Sub Page_Load(sender as object, e as eventArgs)
Try
...All the code from the procedure goes here...
Catch ex as Exception
lblErrorMessage.Text = "Exception Type: " & ex.getType.ToString() & "Exception Source: " & ex.source.ToString()
End Try
End Sub

You could do this for each procedure until you tracked the culprit down. I know this seems like a lot of extra code, but in reality, you should really be using Try...Catch wherever the possibility of exceptions (that are not
preventable) may exist.

Nov 18 '05 #7
Good luck Johah. I think you'll find that it will get easier to incorporate
Try...Catch after you see how easy it is to handle the exceptions and even
develop and throw your own custom exceptions.
"Jonah Olsson" <jo***@IHATESPAM.com> wrote in message
news:ee**************@TK2MSFTNGP12.phx.gbl...
Thanks for you help! I really appreciate it.
And I'm sorry for not being clear enough in my first post.

I think I have to go through all my code then and insert Try/Catch
statements where those are missing.

Again, thanks for you help and time!

Regards,
Jonah Olsson
"Scott M." <s-***@BADSPAMsnet.net> skrev i meddelandet
news:eE**************@TK2MSFTNGP12.phx.gbl...
Ok Jonah, I got you now. Your first post didn't really say that the errors
were being reported by users of the application, rather than the developer of the application.

Well, and I wouldn't recommend this as a normal bit of code, but you could wrap each (or the suspected) procedure's code inside ONE Try statement and have ONE catch that you can interrogate:

Sub Page_Load(sender as object, e as eventArgs)
Try
...All the code from the procedure goes here...
Catch ex as Exception
lblErrorMessage.Text = "Exception Type: " & ex.getType.ToString() &
"Exception Source: " & ex.source.ToString()
End Try
End Sub

You could do this for each procedure until you tracked the culprit down.

I
know this seems like a lot of extra code, but in reality, you should

really
be using Try...Catch wherever the possibility of exceptions (that are

not preventable) may exist.


Nov 18 '05 #8
"Jonah Olsson" <jo***@IHATESPAM.com> wrote in message
news:uQ****************@TK2MSFTNGP12.phx.gbl...
Hi guys,

For my VB.NET application, I created an ErrorHandler class that should save all available information about the last error occurred. I use
Server.GetLastError for this.
The problem is that I quite often get errors that I really can't say why
they happen because of the lack of information from GetLastError. This is an example:

System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.GlobalApp.Application_BeginRequest(Object sender, EventArgs e)
at
System.Web.SyncEventExecutionStep.System.Web.HttpA pplication+IExecutionStep. Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean&
completedSynchronously)

Is this really all I can get from the exception? How can I tell what really happened since our test group hasn't found any bugs when testing the site?


That's all you can get if you don't have debug symbols. With debug symbols,
you'll get the line number and source file.

Is there any other information you're looking for?

Some exceptions do carry more information. For example, an SqlException will
tell you the database server, stored procedure name and line number, and the
SQL Server error code. Your Application_Error handler can check to see what
kind of exception you received and can display any details available.
--
John Saunders
John.Saunders at SurfControl.com
Nov 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Frank | last post by:
Using Python 2.2.2, I want to catch all exceptions from "socket.gethostbyaddr(ip)" From IDLE, I can generate: >>> socket.gethostbyaddr('1.2') Traceback (most recent call last): File...
0
by: Jan Decaluwe | last post by:
Hi: There is a difference between exception info formatting by the interpreter versus the traceback module. For example, say we define an exception Error in file module.py: $ python Python...
3
by: lzh_gladiator | last post by:
Hello How can I get Current Thread Infomation about catched exception. Related Resource will be more helpful. Thanks in advance lzh_gladiator
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
20
by: Tim Reynolds | last post by:
Team, I am developing a web service. In testing in on my enw PC, I am expecting to see exceptions thrown appear on my browser. Instead I am getting an HTTP 500 Internal Server Error page and I am...
4
by: Darren Mar-Elia \(MVP\) | last post by:
So, I have my little .Net 2.0 C# utilities out in the wild and occasionally they have bugs(!). Often the bugs are easy to troubleshoot remotely but occasionally I would like to write detailed trace...
2
by: Richard Coltrane | last post by:
Hi there, Ive just implemented some application level exception handling in ASP.Net 2.0. I deliberately set up a null reference error in my code to see how this would be handled. Sure enough...
2
by: Paolo | last post by:
Hello Everybody! Considering this code: try { ExecuteOperation(); } catch {
0
by: akash | last post by:
Hi, I've got some C# code instantiating a managed C++ class which in turn instantiates an unmanaged C++ class. I'm getting intermittent exceptions caught by my C# code, but the I can't get any...
9
by: Adem | last post by:
Is it possible to get some info about an unknown exception, ie. the "catch (...)" case below: catch (const blah1 &ex) { cout << "blah1 exception." << endl; } catch (const blah2 &ex) {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.