error trapping

Discussion in 'ASP .Net' started by =?Utf-8?B?TWlrZQ==?=, Dec 14, 2004.

  1. 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?
    =?Utf-8?B?TWlrZQ==?=, Dec 14, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?TWlrZQ==?=

    Karl Seguin Guest

    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

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


    "Mike" <> wrote in message
    news:...
    > 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?
    >
    >
    Karl Seguin, Dec 14, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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?
    >
    >
    Eliyahu Goldin, Dec 14, 2004
    #3
  4. "Eliyahu Goldin" <> wrote in message
    news:...
    > 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
    John Saunders, Dec 14, 2004
    #4
  5. 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" <> wrote in message
    > news:...
    > > 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
    >
    >
    >
    =?Utf-8?B?RGVhc3Vu?=, Dec 14, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. toylet

    trapping file i/o error

    toylet, Feb 19, 2004, in forum: Perl
    Replies:
    15
    Views:
    876
    Tassilo v. Parseval
    Feb 23, 2004
  2. Brett Robichaud

    Exceeding File Upload max size - trapping error

    Brett Robichaud, Apr 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,878
    bruce barker
    Apr 7, 2004
  3. Craig Wilson

    Trapping the 404 (resource not found) error

    Craig Wilson, Oct 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    469
    =?Utf-8?B?TGVvbiBXZWxpY2tp?=
    Oct 8, 2004
  4. VB Programmer

    Best standard for error trapping?

    VB Programmer, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    377
    VB Programmer
    Oct 25, 2004
  5. =?Utf-8?B?UGF1bA==?=

    Error Trapping and Error Line Number

    =?Utf-8?B?UGF1bA==?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    470
    Hans Kesting
    Oct 27, 2005
Loading...

Share This Page