error trapping

G

Guest

I have a C# aspx page that calls a C# class file for db access, data, etc.
If one of the "functions" I call in the class has an error how can i pass
that error back to the aspx page and then display it?
 
K

Karl Seguin

Mike:
When you say error, do you mean exception? The two aren't necessarily
interchangable. If your function generates an exception, if you simply
don't catch it, it'll bubble up to the to the aspx page which you can then
handle by explicitly catching or using the OnError event. If, for whatever
reason, you need to catch the exception in the function, you can rethrow it
(check out http://dotnetguy.techieswithcats.com/archives/004118.shtml for
info on how to do that properly)

If you aren't talking about exceptions, simply an error, like the
username/password wasn't found, you can either throw an exception within the
function, throw new ApplicationException("invalid username/password"), or
you can simply using the function's return value. Both methods have
drawbacks...for example, an invalid username/password isnt' really something
exceptional and many people (myself included) would probably be against
using exceptions in this case. Return values can be very messy.

//mix of C# and vb.net so I can type this quicker :)
public function GetUser(string username, string password) as user

DataReader dr = cmd.executeReader()
if dr.Read() then
User u = new User()
u.UserName = dr(...);
return u
else
//what to do?
throw new ApplicationException("invalid username/password");
//or maybe:
return nothing //null in c#
//or maybe
User u = new User();
u.Status = UserStatus.InvalidUser //an enumeration
return u
end function



How you handle it on the page depends on which method you use. For return
values you'll need to check the value and handle it appropriately. For
exceptions you can wrap the call in a Try/Catch or simply rely on the
Page.Error event. You can also use the Global.Asax's Error event to trap
all errors.

Karl
 
E

Eliyahu Goldin

1. How to pass an error.
The standard way is to raise an exception within the data access class. The
page codebehind should wrap data access method calls into try..catch block.
catch block gets the exception message and takes care of displaying it.

2. How to display.
One way is to have a hidden server control capable of displaying text that
will go visible only if there is an error message to show. Another way is to
emit a javascript alert statement that will result in the browser producing
a message box with the message.

Eliyahu
 
J

John Saunders

Eliyahu Goldin said:
1. How to pass an error.
The standard way is to raise an exception within the data access class.
The
page codebehind should wrap data access method calls into try..catch
block.
catch block gets the exception message and takes care of displaying it.

2. How to display.
One way is to have a hidden server control capable of displaying text that
will go visible only if there is an error message to show. Another way is
to
emit a javascript alert statement that will result in the browser
producing
a message box with the message.

What Eliyahu said, but also keep in mind that sometimes just displaying
exceptions isn't a good idea. Your users are likely to ignore them and not
tell you about them. Consider writing full exception details to the system
event log.

John Saunders
 
G

Guest

You could send the error back to yourself thru email or to a IT-Developers
email address. then just give user a nice friendly message if they need one.
This way you get notified of all errors nearly right away!
Writing them to a log is good for backup, incase email server is done or
something. :)
 

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

Similar Threads

Error trapping in global.asax 2
error trapping question 2
Trapping IP address 2
Login form no longer working 2
PHP error 1
Distutils error 2
error trapping 5
Why doesn't the function get called? 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top