.net - How To Catch Exceptions Using CustomErrors
Feb 23, 2010RemoteOnly" defaultRedirect="~/Errors.aspx">
<error statusCode="404" redirect="~/Error.aspx?code=404"/>
</customErrors>
hrow new HttpException(404, "404 Not Found");
RemoteOnly" defaultRedirect="~/Errors.aspx">
<error statusCode="404" redirect="~/Error.aspx?code=404"/>
</customErrors>
hrow new HttpException(404, "404 Not Found");
I have the following settings. Asp.net 2.0 / Vs.net 2005 / Web.config -> customErrors mode="on"
<customErrors mode="On" defaultRedirect="~/ErrorPages/Default.aspx">
If I am just logging exception details in my web app, do I really need to put in exception handling logic for each tier? Why not just let them all bubble up the stack trace to the global.asax and log them there?
View 5 RepliesI'm trying to improve my password change function in my app that allows members to change their passwords. I do have some password rules e.g. min 7 characters, etc.
There's a nice MembershipCreateStatus enumeration in the framework for creating new members. Is there a similar tool for catching password exceptions where the user's newly selected password doesn't match the rules?
I was wondering what will be the method to catch any exception at server end and to show the error message in a div with error icon on top of the page using jquery. All the examples I have browsed show how to display the div on click of some button or link but in my case the scenerio is different. I want to use it for displaying messages to user and make use of Jquery's animations as well.
View 7 RepliesI am fairly new to the asp.net scene. Having build a dashboard gathering info on my OLAP system's health status, I can't seem to figure out how to make sure that I catch the no connection exception.I use the following statements on the datagrid:a) want to change the color of ssas2 button to red if an exception is throw
MyDBConn.ConnectionString =
"Provider=MSOLAP.4;Data Source=core-ssas2;Integrated Security=SSPI;Initial Catalog=Financials;Connect Timeout=6000"
MyDBConn.Open()
MyDBConn.CreateCommand.CommandTimeout = 120
I have a couple of questions on exception handling in .net: 1- Why do we have to catch specific exceptions other than the reason of displaying a particular error to the user? What if we wanted to always display a generic error to the user and not give him much details, would it be okay to catch only generic xceptions?
2- Is throwing exceptions from the data access layer to the UI layer best practice, or is it recommended to return a string or perhaps a boolean values from the data access layer to the UI layer?
Looking for best practice focused answers here with explanations.
Should the presentation layer of an ASP.Net app catch and handle exceptions thrown from the business layer, or should these be allowed to bubble out, where they can all be logged and handled uniformly in the Global.ascx's Application_Error handler?
[code]....
I am fairly new to the asp.net scene. Having build a dashboard gathering info on my OLAP system's health status, I can't seem to figure out how to make sure that I catch the no connection exception.use the following statements on the datagrid:a) want to change the color of ssas2 button to red if an exception is thrown
View 5 RepliesI'm building the standard 3-tier ASP.NET web application but I'm struggling as to where to do certain things - specifically handling exceptions.
I've tried to have a look around on the web for some examples but can't find any which go as far as a whole project showing how everything links together.
In my data-tier I'm connecting to SQL Server and doing some stuff. I know I need to catch exceptions that could be raised as a result but I'm not sure where to do it.
From what I've read I should be doing it in the UI tier but in that case I'm not sure how to ensure that the connection to the database is closed. Is anyone able to clarify how to do this? Also if anyone knows as to where I could find an example 3-tier web application that follows best practices that would be great too.
We have a asp.net application and want to implement logging. The first idea was to use the Application_Error method in the global.asax file.
The problem is that ASP.NET very often seem to throw exceptions internally that are not caused by the application and which seem not to interfer with the users normal workflow. For example we often get HTTPExceptions, UnauthorizedAccessExceptions and others caught in this method, although there is no real error in the application.
Here is the code copied from [URL] In asp.net code behind, I use try-catch try to catch any error but never catch it. In SQL database, if I rename Employees to Employeesx or change column DepartmentID to DepartmentIDx, record will not be deleted (it is right) without any error (it is wrong, suppose catch an error).
CREATE PROCEDURE DeleteDepartment
(
@DepartmentID int
)
AS
BEGIN TRANSACTION
DELETE FROM Employees
WHERE DepartmentID = @DepartmentID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RAISERROR ('Error', 16, 1)
RETURN
END
DELETE FROM Departments
WHERE DepartmentID = @DepartmentID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RAISERROR ('Error', 16, 1)
RETURN
END
OMMIT
I get this error in the browser:
Code:
Thread was being aborted.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Threading.ThreadAbortException: Thread was being aborted.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ThreadAbortException: Thread was being aborted.]
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +486
System.Web.ApplicationStepManager.ResumeSteps(Exception error) +501
System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +123
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +379
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
I have try{}catch(Exception ex){} in the right place:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
// all processing occurs inside here
}
catch(Exception ex)
{
}
}
I even created a global.asax file and on the Application_Error event, I wrote a code that would email me the error (and i'm not getting an email regarding that error when I get the error shown above). I know for a fact that the thread is going inside the "try" statement because I send emails to myself whenever it finishes certain codes inside of it. So how come I'm getting that error in my browser instead of it being handled in my "catch" statement? I have two problems here, one, why is the exception not going to my "catch" statement, and two, why am I getting that error in the first place.
Note: my web app calls a webservice.
I've just uploaded an asp.net web project to a Fasthosts shared server (yes, I know!). The site works fine on a different server, and builds successfully. However, when I upload it (or even just the files in the App_Code folder) to the fasthosts server, I just get a flat error 500 message.
I've ensured that the web.config file includes: <customErrors mode="Off"/> but still nothing - just Error 500.
The Fasthosts server is running asp.net 3.5, which is what the web project is built to.
Here is my customErrors section:
<customErrors mode="On" defaultRedirect="~/ServerError.aspx">
<error statusCode="403" redirect="~/NotAllowed.aspx" />
<error statusCode="404" redirect="~/404.aspx" />
<error statusCode="500" redirect="~/ServerError.aspx" />
</customErrors>
When an error occurs (I type in a non-existing page in address), I get the default IE "page not found" page.This happens both on localhost in development, and of course in live environment.I think this started happening since a recent Microsoft Update, which included most recent updates to .net.
We developed an application in ASP.NET 3.5 on Windows Server 2003. (IIS 6.0)They are trying to deploy it on Windows Server 2008 IIS 7.5.But we are getting the old yellow screen message: Runtime ErrorDescription:
[Code]....
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
[Code]....
An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".We have turned off custom errors everywhere we can find in ASP.Net Error Pages and set it to Detailed Errors at all folder levels for IIS Error Pages.Still the yellow screen with custom errors and no detailed errors.If we switch the application pool to run in 32 bit = true, we get a detailed error. But we don't get very far with that because of Oracle issues and wouldn't really want to force it to run in 32 bit when it should really run in 64 bit anyway.So how do we get detailed messages from IIS while running in 64 bit?
I successfully implemented role based authorization in ASP.NET. When a person does not have the needed role he gets to see an error page for 401.2 not authorized.
What I would like to accomplish now is to have a custom 401 page in my application and have it redirected there via settings in the web.config. I tried this:
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="401" redirect="NoAccess.htm" />
</customErrors>
But this does not get caught. Do I have to override it in IIS instead? I hope not as that would make getting things deployed harder.
If I have a customErrors section in my Web.config that says to redirect to Error.html, then putting code in the Application_Error method in the Global.asax to redirect to Error.html is redundant is it not? Technically, I could bypass the Web.config by redirecting to a different page in the Application_Error method if I wanted to, but since I don't want to go to a separate page I don't think I need the code.
View 2 RepliesASP.NET CustomErrors ignores when a dot before slash is in url
View 1 RepliesWe usually catch unhandled exceptions in Global.asax, and then we redirect to a nice friendly error page. This is fine for the Live environment, but in our development environment we would like to check if CustomErrors are Off, and if so, just throw the ugly error.
Is there an easy way to check if CustomErrors are Off through code?
To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".i got this error during login time on the server how it can be solved
View 1 RepliesThe basic idea is we have a test enviroment which mimics Production so customErrors="RemoteOnly". We just built a test harness that runs against the Test enviroment and detects breaks. We would like it to be able to pull back the detailed error. But we don't want to turn customErrors="On" because then it doesn't mimic Production.
I've looked around and thought a lot, and everything I've come up with isn't possible. Am I wrong about any of these points?
We can't turn customErrors on at runtime because when you call configuration.Save() - it writes the web.config to disk and now it's Off for every request.We can't symlink the files into a new top level directory with it's own web.config because we're on windows and subversion on windows doesn't do symlinks.
We can't use URL-Mapping to make an empty folder dir2 with its own web.config and make the files in dir1 appear to be in dir2 - the web.config doesn't apply. We can't copy all the aspx files into dir2 with it's own web.config because none of the links would be consistent and it's a horrible hacky solution.
We can't change customErrors in web.config based on hostname (e.g. add another dns entry to the test server) because it's not possible/supported. We can't do any virtual directory shenanigans to make it work.
If I'm not, is there a way to accomplish what I'm trying to do? Turn on customErrors site-wide under certain circumstances (dns name or even a querystring value)?
By defauly in my web.config I have set the , in runtime I just want to change the Mode = "Off" in memory. I do not want to save the changes to web.config.
Basically we need to see the description of the runtime error, when required.
In my ASP.NET 3.5 Website which is published in shared hosting provider , I've configured my web.config file like this :
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="AccessDenied.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
[code]...
This is a yellow page which is not user friendly and we didn't expect . I'm wondering setting customeError in webconfig doesn't support this type of address or not ? How can i prevent users seeing this yellow page . Edit : solution you mentioned are about configuring IIS ,But as i mentioned earlier , my site has been published on shared hosting provider . I don't have those access at IIS , What should i do at this situation ?
My Web.config customErrors section is given below
<customErrors
mode="RemoteOnly"
defaultRedirect="error.htm"></customErrors>
How can I exclude a aspx page from DefaultRedirect, if errors occur in that page?