Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > error trapping

Reply
Thread Tools

error trapping

 
 
=?Utf-8?B?TWlrZQ==?=
Guest
Posts: n/a
 
      12-14-2004
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?


 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      12-14-2004
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...s/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

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
>



 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      12-14-2004
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

"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
>



 
Reply With Quote
 
John Saunders
Guest
Posts: n/a
 
      12-14-2004
"Eliyahu Goldin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
=?Utf-8?B?RGVhc3Vu?=
Guest
Posts: n/a
 
      12-14-2004
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.


"John Saunders" wrote:

> "Eliyahu Goldin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Trapping and Error Line Number =?Utf-8?B?UGF1bA==?= ASP .Net 3 10-27-2005 11:49 AM
Best standard for error trapping? VB Programmer ASP .Net 2 10-25-2004 08:49 PM
Trapping the 404 (resource not found) error Craig Wilson ASP .Net 2 10-08-2004 08:45 PM
Exceeding File Upload max size - trapping error Brett Robichaud ASP .Net 1 04-07-2004 10:29 PM
trapping file i/o error toylet Perl 15 02-23-2004 09:16 AM



Advertisments