Handling Exceptions in an n-tier environment

Discussion in 'ASP .Net' started by Raterus, Aug 24, 2004.

  1. Raterus

    Raterus Guest

    Hello,

    I'm trying to hop on the n-tier/OOP bandwagon for my applications, but I've hit one snag that I'm not sure the best way to proceed. Say a SqlException is raised in my Data Access layer, that is a long long way from my presentation layer/aspx page, where I would at least like to print out a notification that an error was raised. How can I best achieve this in an n-tier environment. It seems if I handle the error in my Data Access layer, by the time the code execution gets back to the presentation layer, it won't know what happened. Should I be defining my own object exceptions, and raise them when I catch an error, and let it propigate itself up the pipeline until the presentation layer can handle it?

    Any help would be greatly appreciated!
    --Michael
    Raterus, Aug 24, 2004
    #1
    1. Advertising

  2. Raterus

    Karl Guest

    Statement: "It seems if I handle the error in my DAL"
    Question: Can you actually handle the error (like, hit a backup database, or
    hit a cache which might be stale but will give some information) or can you
    just swallow it?

    I ask because my guess is that you can't actually handle it at the DAL
    layer - all you can do is catch it at the presentation layer and display a
    friendly error message. At that point, you have two options:

    1 - let the exception bubble up (either don't catch it, or have an empty
    throw (don't do a Throw ex))
    2 - If, and only if, you can add additional information to the exception,
    rethrow a new exception (custom or not) making sure to include the original
    exception in there. (throw new ApplicationException("Doh",
    originalException))

    Karl


    "Raterus" <> wrote in message
    news:...
    Hello,

    I'm trying to hop on the n-tier/OOP bandwagon for my applications, but I've
    hit one snag that I'm not sure the best way to proceed. Say a SqlException
    is raised in my Data Access layer, that is a long long way from my
    presentation layer/aspx page, where I would at least like to print out a
    notification that an error was raised. How can I best achieve this in an
    n-tier environment. It seems if I handle the error in my Data Access layer,
    by the time the code execution gets back to the presentation layer, it won't
    know what happened. Should I be defining my own object exceptions, and
    raise them when I catch an error, and let it propigate itself up the
    pipeline until the presentation layer can handle it?

    Any help would be greatly appreciated!
    --Michael
    Karl, Aug 24, 2004
    #2
    1. Advertising

  3. This is often handled exactly how you describe at the end of your post. The
    data layer SqlException bubbles up to the Business Object layer which then
    wraps that exception and re-raises with a more friendly strongly-typed
    exception like OrderAddException when trying to call Order.Add. The
    presentation layer (.aspx pages) then handles the OrderAddException and
    presents a friendly message to the user like "Could not place your Order.
    Please contact support here...."

    --
    Hope this helps,
    Bryant Hankins
    Numinet Systems Inc.
    http://www.numinet.com/blogging



    "Raterus" <> wrote in message
    news:...
    Hello,

    I'm trying to hop on the n-tier/OOP bandwagon for my applications, but I've
    hit one snag that I'm not sure the best way to proceed. Say a SqlException
    is raised in my Data Access layer, that is a long long way from my
    presentation layer/aspx page, where I would at least like to print out a
    notification that an error was raised. How can I best achieve this in an
    n-tier environment. It seems if I handle the error in my Data Access layer,
    by the time the code execution gets back to the presentation layer, it won't
    know what happened. Should I be defining my own object exceptions, and
    raise them when I catch an error, and let it propigate itself up the
    pipeline until the presentation layer can handle it?

    Any help would be greatly appreciated!
    --Michael
    Bryant Hankins, Aug 24, 2004
    #3
  4. Raterus

    Raterus Guest

    That makes a lot of sense, I was actually experimenting with this method before I received any answers, and it works very well. Thanks!

    "Bryant Hankins" <bryanthankins@_NO_SPAM_hotmail.com> wrote in message news:%...
    > This is often handled exactly how you describe at the end of your post. The
    > data layer SqlException bubbles up to the Business Object layer which then
    > wraps that exception and re-raises with a more friendly strongly-typed
    > exception like OrderAddException when trying to call Order.Add. The
    > presentation layer (.aspx pages) then handles the OrderAddException and
    > presents a friendly message to the user like "Could not place your Order.
    > Please contact support here...."
    >
    > --
    > Hope this helps,
    > Bryant Hankins
    > Numinet Systems Inc.
    > http://www.numinet.com/blogging
    >
    >
    >
    > "Raterus" <> wrote in message
    > news:...
    > Hello,
    >
    > I'm trying to hop on the n-tier/OOP bandwagon for my applications, but I've
    > hit one snag that I'm not sure the best way to proceed. Say a SqlException
    > is raised in my Data Access layer, that is a long long way from my
    > presentation layer/aspx page, where I would at least like to print out a
    > notification that an error was raised. How can I best achieve this in an
    > n-tier environment. It seems if I handle the error in my Data Access layer,
    > by the time the code execution gets back to the presentation layer, it won't
    > know what happened. Should I be defining my own object exceptions, and
    > raise them when I catch an error, and let it propigate itself up the
    > pipeline until the presentation layer can handle it?
    >
    > Any help would be greatly appreciated!
    > --Michael
    >
    >
    Raterus, Aug 24, 2004
    #4
  5. Raterus

    Raterus Guest

    hmm, Is there a good reason why I shouldn't issue a throw ex?

    Try
    somethingThatCanCauseAnError()
    Catch ex As Exception
    Throw ex
    End Try

    I can understand creating a new ApplicationException with additional details, but sometimes all I need is the original exception details.

    Thanks for your help,
    --Michael

    "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:...
    > Statement: "It seems if I handle the error in my DAL"
    > Question: Can you actually handle the error (like, hit a backup database, or
    > hit a cache which might be stale but will give some information) or can you
    > just swallow it?
    >
    > I ask because my guess is that you can't actually handle it at the DAL
    > layer - all you can do is catch it at the presentation layer and display a
    > friendly error message. At that point, you have two options:
    >
    > 1 - let the exception bubble up (either don't catch it, or have an empty
    > throw (don't do a Throw ex))
    > 2 - If, and only if, you can add additional information to the exception,
    > rethrow a new exception (custom or not) making sure to include the original
    > exception in there. (throw new ApplicationException("Doh",
    > originalException))
    >
    > Karl
    >
    >
    > "Raterus" <> wrote in message
    > news:...
    > Hello,
    >
    > I'm trying to hop on the n-tier/OOP bandwagon for my applications, but I've
    > hit one snag that I'm not sure the best way to proceed. Say a SqlException
    > is raised in my Data Access layer, that is a long long way from my
    > presentation layer/aspx page, where I would at least like to print out a
    > notification that an error was raised. How can I best achieve this in an
    > n-tier environment. It seems if I handle the error in my Data Access layer,
    > by the time the code execution gets back to the presentation layer, it won't
    > know what happened. Should I be defining my own object exceptions, and
    > raise them when I catch an error, and let it propigate itself up the
    > pipeline until the presentation layer can handle it?
    >
    > Any help would be greatly appreciated!
    > --Michael
    >
    >
    Raterus, Aug 24, 2004
    #5
  6. Raterus

    Karl Guest

    Throw ex rebundles the exception...I agree that what you want is the
    original exception, that's what plain Throw does ...

    If the exception thrown was an SqlException,you'll rethrow an Exception.
    The original exception is still available in InnerException...but since you
    you havent' added your own information, all you've managed to do is
    obfuscate the important stuff.

    Check out: http://dotnetguy.techieswithcats.com/archives/004118.shtml for a
    great explanation :)

    Karl

    "Raterus" <> wrote in message
    news:...
    hmm, Is there a good reason why I shouldn't issue a throw ex?

    Try
    somethingThatCanCauseAnError()
    Catch ex As Exception
    Throw ex
    End Try

    I can understand creating a new ApplicationException with additional
    details, but sometimes all I need is the original exception details.

    Thanks for your help,
    --Michael

    "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
    message news:...
    > Statement: "It seems if I handle the error in my DAL"
    > Question: Can you actually handle the error (like, hit a backup database,

    or
    > hit a cache which might be stale but will give some information) or can

    you
    > just swallow it?
    >
    > I ask because my guess is that you can't actually handle it at the DAL
    > layer - all you can do is catch it at the presentation layer and display a
    > friendly error message. At that point, you have two options:
    >
    > 1 - let the exception bubble up (either don't catch it, or have an empty
    > throw (don't do a Throw ex))
    > 2 - If, and only if, you can add additional information to the exception,
    > rethrow a new exception (custom or not) making sure to include the

    original
    > exception in there. (throw new ApplicationException("Doh",
    > originalException))
    >
    > Karl
    >
    >
    > "Raterus" <> wrote in message
    > news:...
    > Hello,
    >
    > I'm trying to hop on the n-tier/OOP bandwagon for my applications, but

    I've
    > hit one snag that I'm not sure the best way to proceed. Say a

    SqlException
    > is raised in my Data Access layer, that is a long long way from my
    > presentation layer/aspx page, where I would at least like to print out a
    > notification that an error was raised. How can I best achieve this in an
    > n-tier environment. It seems if I handle the error in my Data Access

    layer,
    > by the time the code execution gets back to the presentation layer, it

    won't
    > know what happened. Should I be defining my own object exceptions, and
    > raise them when I catch an error, and let it propigate itself up the
    > pipeline until the presentation layer can handle it?
    >
    > Any help would be greatly appreciated!
    > --Michael
    >
    >
    Karl, Aug 24, 2004
    #6
  7. Raterus

    Raterus Guest

    Good to know! Thanks, you have been very helpful!
    --Michael

    "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:...
    > Throw ex rebundles the exception...I agree that what you want is the
    > original exception, that's what plain Throw does ...
    >
    > If the exception thrown was an SqlException,you'll rethrow an Exception.
    > The original exception is still available in InnerException...but since you
    > you havent' added your own information, all you've managed to do is
    > obfuscate the important stuff.
    >
    > Check out: http://dotnetguy.techieswithcats.com/archives/004118.shtml for a
    > great explanation :)
    >
    > Karl
    >
    > "Raterus" <> wrote in message
    > news:...
    > hmm, Is there a good reason why I shouldn't issue a throw ex?
    >
    > Try
    > somethingThatCanCauseAnError()
    > Catch ex As Exception
    > Throw ex
    > End Try
    >
    > I can understand creating a new ApplicationException with additional
    > details, but sometimes all I need is the original exception details.
    >
    > Thanks for your help,
    > --Michael
    >
    > "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
    > message news:...
    > > Statement: "It seems if I handle the error in my DAL"
    > > Question: Can you actually handle the error (like, hit a backup database,

    > or
    > > hit a cache which might be stale but will give some information) or can

    > you
    > > just swallow it?
    > >
    > > I ask because my guess is that you can't actually handle it at the DAL
    > > layer - all you can do is catch it at the presentation layer and display a
    > > friendly error message. At that point, you have two options:
    > >
    > > 1 - let the exception bubble up (either don't catch it, or have an empty
    > > throw (don't do a Throw ex))
    > > 2 - If, and only if, you can add additional information to the exception,
    > > rethrow a new exception (custom or not) making sure to include the

    > original
    > > exception in there. (throw new ApplicationException("Doh",
    > > originalException))
    > >
    > > Karl
    > >
    > >
    > > "Raterus" <> wrote in message
    > > news:...
    > > Hello,
    > >
    > > I'm trying to hop on the n-tier/OOP bandwagon for my applications, but

    > I've
    > > hit one snag that I'm not sure the best way to proceed. Say a

    > SqlException
    > > is raised in my Data Access layer, that is a long long way from my
    > > presentation layer/aspx page, where I would at least like to print out a
    > > notification that an error was raised. How can I best achieve this in an
    > > n-tier environment. It seems if I handle the error in my Data Access

    > layer,
    > > by the time the code execution gets back to the presentation layer, it

    > won't
    > > know what happened. Should I be defining my own object exceptions, and
    > > raise them when I catch an error, and let it propigate itself up the
    > > pipeline until the presentation layer can handle it?
    > >
    > > Any help would be greatly appreciated!
    > > --Michael
    > >
    > >

    >
    >
    Raterus, Aug 24, 2004
    #7
    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. rob

    ASP v2 & 3-tier or 2-tier

    rob, Aug 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    Steve C. Orr [MVP, MCSD]
    Aug 13, 2004
  2. NOSPAM

    2 tier to 3 tier?

    NOSPAM, Oct 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    425
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Oct 14, 2004
  3. Steve Kershaw

    Setting up a new tier in a 3-tier system?

    Steve Kershaw, Mar 28, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    443
    Steve Kershaw
    Mar 29, 2006
  4. Shantanu Bhattacharya
    Replies:
    2
    Views:
    547
    Christopher Benson-Manica
    Dec 26, 2003
  5. Shantanu Bhattacharya

    Converting a 2-tier application to 3-tier application

    Shantanu Bhattacharya, Dec 25, 2003, in forum: C Programming
    Replies:
    2
    Views:
    455
    Christopher Benson-Manica
    Dec 26, 2003
Loading...

Share This Page