General ExceptionHandler in Asp.Net - Page_Error?

G

Guest

Hi!
I want to set up an exception handler in my asp.net page which handles all
unhandled exception. When the exception is trapped I want to assign its
message to a label. How can I do this?

I have tryied to do like this:

Protected Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Error
Label.text = Server.GetLastError().Message()
Server.ClearError()
End Sub

The exception is trapped but I got a blank page. Why is that? Is it
possible to do what i´m trying to do?
 
T

Teemu Keiski

Hi,

the problem most likely is that the error causes the page to stop the
execution and just to raise the error handler up (processing does not
continue to rendering etc), therefore it is visible like this.

If you want to let the execution continue, you'd need to catch exceptions at
blobk/method level.
 
B

Brock Allen

Well, I'm not sure why your Label isn't working, but I do have a comment
about the approach in general. Once this goes to production, I'd suggest
not doing this. The reason is that you shouldn't show exception information
to an end user. If the end user is an attacker (malicious) then they can
use that information against you. Instead, you should only ever show vague
information that "there was some sort of problem". Of course, log that in
your global.asax's Application_Error method so an admin has all the info,
but don't give it to your users.
 
G

Guest

Hi, I would try doing a Try...Catch...End Try method. your catch could be:

cath e as exception
label.text ="text"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top