You must Sign In to post a response.
Category: ASP.NET
#765125
Hi,
You can handle application-level errors in Global.asax file as follows:
void Application_Error(object sender, EventArgs e)// Code runs when unhandled error occurs in application
{
// Get the last exception .
Exception excp = Server.GetLastError();
Server.Transfer("ErrorPage.aspx");
// OR
// Show error message on error page
Response.Write("Error\n");
Response.Write(exc.Message +"</p>\n");
// Log exception and notify system
ExceptionUtility.LogException(exc, "ErrorPage");
ExceptionUtility.NotifySystemOps(exc);
}
Hope it helps.
Regards,
Shashikant Gurav
shashikantgurav22@gmail.com
You can handle application-level errors in Global.asax file as follows:
void Application_Error(object sender, EventArgs e)// Code runs when unhandled error occurs in application
{
// Get the last exception .
Exception excp = Server.GetLastError();
Server.Transfer("ErrorPage.aspx");
// OR
// Show error message on error page
Response.Write("Error\n");
Response.Write(exc.Message +"</p>\n");
// Log exception and notify system
ExceptionUtility.LogException(exc, "ErrorPage");
ExceptionUtility.NotifySystemOps(exc);
}
Hope it helps.
Regards,
Shashikant Gurav
shashikantgurav22@gmail.com
#765126
Thanks shashikant,for ur help.I would like to ask u.Suppose there 2 user acessing 2 diff page,both got error,User1 get user1's error or user2's error.
I am confuse because of following line.
Exception excp = Server.GetLastError();
I am confuse because of following line.
Exception excp = Server.GetLastError();
#765129
Hi,
whenever User1 get error on Page1, server will return Page1's error and whenever User2 get error on Page2, sever returns error from Page2.
Though Global.asax is an Application specific file, user specific session are alloted by asp.net so for that session/instance this Server.GetLastError() will return specific error details.
You can get detailed source of error using following code:
Exception lastError = Server.GetLastError() as HttpException;
string lastErrorType = lastError.GetType().ToString();
string lastErrorMsg = lastError.Message;
string lastErrorStackTrace = lastError.StackTrace;
Or you can try:
Exception lastError = (Server.GetLastError() as HttpException).InnerException;
Regards,
Shashikant Gurav
shashikantgurav22@gmail.com
whenever User1 get error on Page1, server will return Page1's error and whenever User2 get error on Page2, sever returns error from Page2.
Though Global.asax is an Application specific file, user specific session are alloted by asp.net so for that session/instance this Server.GetLastError() will return specific error details.
You can get detailed source of error using following code:
Exception lastError = Server.GetLastError() as HttpException;
string lastErrorType = lastError.GetType().ToString();
string lastErrorMsg = lastError.Message;
string lastErrorStackTrace = lastError.StackTrace;
Or you can try:
Exception lastError = (Server.GetLastError() as HttpException).InnerException;
Regards,
Shashikant Gurav
shashikantgurav22@gmail.com
#765137
When you are not write TRY...CATCH in page or function and if any code throws exception then it will goes in global.asax file in Application_Error event
Look at the following code, This code example shows how to create an error handler in the Global.asax file that will catch all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler. In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
Exception exc = Server.GetLastError();
// Handle HTTP errors
if (exc.GetType() == typeof(HttpException))
{
// The Complete Error Handling Example generates
// some errors using URLs with "NoCatch" in them;
// ignore these here to simulate what would happen
// if a global.asax handler were not implemented.
if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
return;
//Redirect HTTP errors to HttpError page
Server.Transfer("HttpErrorPage.aspx");
}
// For other kinds of errors give the user some information
// but stay on the default page
Response.Write("<h2>Global Page Error</h2>\n");
Response.Write(
"<p>" + exc.Message + "</p>\n");
Response.Write("Return to the <a href='Default.aspx'>" +
"Default Page</a>\n");
// Log the exception and notify system operators
ExceptionUtility.LogException(exc, "DefaultPage");
ExceptionUtility.NotifySystemOps(exc);
// Clear the error from the server
Server.ClearError();
}
Thanks
Koolprasd2003
Editor, DotNetSpider MVM
Microsoft MVP 2014 [ASP.NET/IIS]
Look at the following code, This code example shows how to create an error handler in the Global.asax file that will catch all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler. In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
Exception exc = Server.GetLastError();
// Handle HTTP errors
if (exc.GetType() == typeof(HttpException))
{
// The Complete Error Handling Example generates
// some errors using URLs with "NoCatch" in them;
// ignore these here to simulate what would happen
// if a global.asax handler were not implemented.
if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
return;
//Redirect HTTP errors to HttpError page
Server.Transfer("HttpErrorPage.aspx");
}
// For other kinds of errors give the user some information
// but stay on the default page
Response.Write("<h2>Global Page Error</h2>\n");
Response.Write(
"<p>" + exc.Message + "</p>\n");
Response.Write("Return to the <a href='Default.aspx'>" +
"Default Page</a>\n");
// Log the exception and notify system operators
ExceptionUtility.LogException(exc, "DefaultPage");
ExceptionUtility.NotifySystemOps(exc);
// Clear the error from the server
Server.ClearError();
}
Thanks
Koolprasd2003
Editor, DotNetSpider MVM
Microsoft MVP 2014 [ASP.NET/IIS]
#765141
1. Write the error log file in your application. It will be safe for know the error
2. You can handle the error page in the web.config. If any error occurred in your application the page will be redirected to the error page which was designed by you.
By Nathan
Direction is important than speed
2. You can handle the error page in the web.config. If any error occurred in your application the page will be redirected to the error page which was designed by you.
<configuration>
<system.web>
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly">
<error statusCode="500"
redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
By Nathan
Direction is important than speed
Return to Return to Discussion Forum