Error Handling with response.write

Discussion in 'ASP .Net' started by Michael, Dec 26, 2003.

  1. Michael

    Michael Guest

    Hello,
    I have a separate Database class that handles any database work that all my
    asp.net pages can use.

    My problem is, many times I use try/catch to catch errors, and I want to
    output these errors to the webpage with response.write().

    Unfortunately Response.anything from within my class generate a "Response is
    not declared" error. How can I get Response to work from within a class, or
    is there a better way to handle errors in asp.net classes.

    Thanks,
    --Michael
    Michael, Dec 26, 2003
    #1
    1. Advertising

  2. Michael

    Hermit Dave Guest

    michael,

    well try not handling the exception at that point instead use a global error
    handler something like application_error
    which can check the type of exception and do a server.transfer
    (response.redirect would clear the error) to appropraite page to show the
    error
    or if you like to display it with calling page.... try wrapping a try catch
    block when you the the database object.
    catch the exception from within your aspx page

    hope this helps

    hd

    "Michael" <raterus@localhost> wrote in message
    news:...
    > Hello,
    > I have a separate Database class that handles any database work that all

    my
    > asp.net pages can use.
    >
    > My problem is, many times I use try/catch to catch errors, and I want to
    > output these errors to the webpage with response.write().
    >
    > Unfortunately Response.anything from within my class generate a "Response

    is
    > not declared" error. How can I get Response to work from within a class,

    or
    > is there a better way to handle errors in asp.net classes.
    >
    > Thanks,
    > --Michael
    >
    >
    Hermit Dave, Dec 26, 2003
    #2
    1. Advertising

  3. Michael

    Scott M. Guest

    Don't have the class attempt to provide any kind of user interface.
    Instead, use a Try...Catch and in the Catch, throw a new exception that will
    bubble up to the applicaiton that is calling the class. This applicaiton
    hand then display details about the exception in its interface.


    "Hermit Dave" <> wrote in message
    news:%23xl4$...
    > michael,
    >
    > well try not handling the exception at that point instead use a global

    error
    > handler something like application_error
    > which can check the type of exception and do a server.transfer
    > (response.redirect would clear the error) to appropraite page to show the
    > error
    > or if you like to display it with calling page.... try wrapping a try

    catch
    > block when you the the database object.
    > catch the exception from within your aspx page
    >
    > hope this helps
    >
    > hd
    >
    > "Michael" <raterus@localhost> wrote in message
    > news:...
    > > Hello,
    > > I have a separate Database class that handles any database work that all

    > my
    > > asp.net pages can use.
    > >
    > > My problem is, many times I use try/catch to catch errors, and I want to
    > > output these errors to the webpage with response.write().
    > >
    > > Unfortunately Response.anything from within my class generate a

    "Response
    > is
    > > not declared" error. How can I get Response to work from within a

    class,
    > or
    > > is there a better way to handle errors in asp.net classes.
    > >
    > > Thanks,
    > > --Michael
    > >
    > >

    >
    >
    Scott M., Dec 26, 2003
    #3
  4. Michael

    Kikoz Guest

    Another solution depends on how separate that class should
    be from asp.net context. If it doesn't matter, then you
    can simply pass Response to this class' constructor and
    use it inside of it. Will work just fine.

    Regards.


    >-----Original Message-----
    >Hello,
    >I have a separate Database class that handles any

    database work that all my
    >asp.net pages can use.
    >
    >My problem is, many times I use try/catch to catch

    errors, and I want to
    >output these errors to the webpage with response.write().
    >
    >Unfortunately Response.anything from within my class

    generate a "Response is
    >not declared" error. How can I get Response to work from

    within a class, or
    >is there a better way to handle errors in asp.net classes.
    >
    >Thanks,
    >--Michael
    >
    >
    >.
    >
    Kikoz, Dec 26, 2003
    #4
  5. Try something like this. Theoratically this should work

    Your code

    try
    {
    Code .....
    }
    catch (Exception e)
    {
    WriteException ( e);
    }

    Another function ...

    WriteException (Exception e)
    {
    if (HttpContext.Current!=nothing)
    {
    HttpContext.Current.Response.Write (ex.Message)
    }
    else
    {
    // Skip since we are not running from ASP.NET
    }

    }

    Thanks
    Shiplu

    On Fri, 26 Dec 2003 11:42:40 -0500, Scott M. <> wrote:

    > Don't have the class attempt to provide any kind of user interface.
    > Instead, use a Try...Catch and in the Catch, throw a new exception that
    > will
    > bubble up to the applicaiton that is calling the class. This applicaiton
    > hand then display details about the exception in its interface.
    >
    >
    > "Hermit Dave" <> wrote in message
    > news:%23xl4$...
    >> michael,
    >>
    >> well try not handling the exception at that point instead use a global

    > error
    >> handler something like application_error
    >> which can check the type of exception and do a server.transfer
    >> (response.redirect would clear the error) to appropraite page to show
    >> the
    >> error
    >> or if you like to display it with calling page.... try wrapping a try

    > catch
    >> block when you the the database object.
    >> catch the exception from within your aspx page
    >>
    >> hope this helps
    >>
    >> hd
    >>
    >> "Michael" <raterus@localhost> wrote in message
    >> news:...
    >> > Hello,
    >> > I have a separate Database class that handles any database work that

    >> all
    >> my
    >> > asp.net pages can use.
    >> >
    >> > My problem is, many times I use try/catch to catch errors, and I want

    >> to
    >> > output these errors to the webpage with response.write().
    >> >
    >> > Unfortunately Response.anything from within my class generate a

    > "Response
    >> is
    >> > not declared" error. How can I get Response to work from within a

    > class,
    >> or
    >> > is there a better way to handle errors in asp.net classes.
    >> >
    >> > Thanks,
    >> > --Michael
    >> >
    >> >

    >>
    >>

    >
    >




    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
    Shafqat Ahmed, Dec 29, 2003
    #5
  6. Michael

    Alvin Bruney Guest

    > WriteException (Exception e)
    > {
    > if (HttpContext.Current!=nothing)
    > {
    > HttpContext.Current.Response.Write (ex.Message)
    > }
    > else
    > {
    > // Skip since we are not running from ASP.NET
    > }


    This code, though well intentioned, will not work as intended because the
    context object is always null outside of the page - as in a user class for
    example. What is needed is a way to pass a reference of the context object
    to the class library.

    I posted this to another newsgroup. It applies here as well.

    You can pass in the context object which houses the request and response
    object for an application to your method. Your method will contain the logic
    to manipulate the response and request objects. Consider:

    private void somefunction(System.Web.HttpContext Stream)
    {

    Stream.Session["session_id"] = "value";

    }

    > >> > Unfortunately Response.anything from within my class generate a

    > > "Response
    > >> is
    > >> > not declared" error. How can I get Response to work from within a

    > > class,


    Armed with this general idea, you can adapt your class to take a reference
    to the context object when it is instantiated. When you need to touch the
    response object, you will touch it thru the reference provided in your
    class. As always, be careful to properly release resources when they aren't
    needed.

    --
    Regards,
    Alvin Bruney
    Got DotNet? Get it here
    http://home.networkip.net/dotnet/tidbits/default.htm
    "Shafqat Ahmed" <> wrote in message
    news:eek:...
    > Try something like this. Theoratically this should work
    >
    > Your code
    >
    > try
    > {
    > Code .....
    > }
    > catch (Exception e)
    > {
    > WriteException ( e);
    > }
    >
    > Another function ...
    >
    > WriteException (Exception e)
    > {
    > if (HttpContext.Current!=nothing)
    > {
    > HttpContext.Current.Response.Write (ex.Message)
    > }
    > else
    > {
    > // Skip since we are not running from ASP.NET
    > }
    >
    > }
    >
    > Thanks
    > Shiplu
    >
    > On Fri, 26 Dec 2003 11:42:40 -0500, Scott M. <>

    wrote:
    >
    > > Don't have the class attempt to provide any kind of user interface.
    > > Instead, use a Try...Catch and in the Catch, throw a new exception that
    > > will
    > > bubble up to the applicaiton that is calling the class. This

    applicaiton
    > > hand then display details about the exception in its interface.
    > >
    > >
    > > "Hermit Dave" <> wrote in

    message
    > > news:%23xl4$...
    > >> michael,
    > >>
    > >> well try not handling the exception at that point instead use a global

    > > error
    > >> handler something like application_error
    > >> which can check the type of exception and do a server.transfer
    > >> (response.redirect would clear the error) to appropraite page to show
    > >> the
    > >> error
    > >> or if you like to display it with calling page.... try wrapping a try

    > > catch
    > >> block when you the the database object.
    > >> catch the exception from within your aspx page
    > >>
    > >> hope this helps
    > >>
    > >> hd
    > >>
    > >> "Michael" <raterus@localhost> wrote in message
    > >> news:...
    > >> > Hello,
    > >> > I have a separate Database class that handles any database work that
    > >> all
    > >> my
    > >> > asp.net pages can use.
    > >> >
    > >> > My problem is, many times I use try/catch to catch errors, and I want
    > >> to
    > >> > output these errors to the webpage with response.write().
    > >> >
    > >> > Unfortunately Response.anything from within my class generate a

    > > "Response
    > >> is
    > >> > not declared" error. How can I get Response to work from within a

    > > class,
    > >> or
    > >> > is there a better way to handle errors in asp.net classes.
    > >> >
    > >> > Thanks,
    > >> > --Michael
    > >> >
    > >> >
    > >>
    > >>

    > >
    > >

    >
    >
    >
    > --
    > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
    Alvin Bruney, Dec 29, 2003
    #6
    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. Charles A. Lackman
    Replies:
    2
    Views:
    7,975
    Charles A. Lackman
    Jan 11, 2006
  2. Matthew Wieder

    Response.Write and Response.Redirect

    Matthew Wieder, Jul 17, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    677
    Kevin Spencer
    Jul 17, 2003
  3. Sehboo
    Replies:
    3
    Views:
    8,762
    Sehboo
    Dec 10, 2003
  4. Replies:
    1
    Views:
    819
  5. slawekg

    Response.Write and Response.Redirect

    slawekg, Jun 21, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    366
    Alexey Smirnov
    Jun 21, 2007
Loading...

Share This Page