Error Handling with response.write

M

Michael

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
 
H

Hermit Dave

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
 
S

Scott M.

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.
 
K

Kikoz

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.
 
S

Shafqat Ahmed

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
 
A

Alvin Bruney

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";

}

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 said:
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
 

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


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top