C# - Exception Handling - Implement Custom Exception Classes?

Jul 2, 2010

i have this application structure:

1. Presentation Layer which calls

2. Business Logic Layer which in turn calls

3. Data Access Layer to do the dealing with the database.

Now i have a Contacts page from where i can add a new contact to the database.So to add a New Contact i fill in all the required details and then call a Add Method (residing in the BLL) from the page, which in turn call a Add Method residing in the DAL.this method in the DAL returns the Current Identity of the record which is then return back to the BLL method and finally delivered on the page.this is fine. but what if a get an exception how do i handle it properly because the method in DAL has a return type of int and i dont want to throw another error!! coz other wise i will have to write try catch in almost all the methods.

//something like this
public int AddMethod(ContactClass contactObj)
{
int result = 0;[code]...

rather i want to show the user a user-friendly message which they can easily understand and in the mean while i will send a mail to myself documenting the Error that just occurred.how can i implement my custom exception classes.

I am trying to write a webservice that's going to be used through ASP.Net Ajax. In this webservice I have to add exception handling so that in case of any error, I could log them to some central place and then throw.

In my other areas of code (like aspx pages), i have been using a basepage which Overrides System.Web.UI.Page class's OnError by adding a custom event handler. I want to do the same for my webservice as well.

Basically I do not want to decorate each webservice method with a Try Catch block. Instead if there is a clean way like to override the on error method, i want to go for that. Unfortunately the WebService class does not seem to have any functionality for this.

I have a web page with 5 textboxes (for branch_id, street, city, state, and zip) and a button. When data is entered in the textboxes, the button is clicked and the data is populated in the database. I have the sql INSERT statement ("insert into branch values(:branch_id, location_type(:street, :city, :state, :zip), staff_nested())") in both the SELECT command and INSERT command. It was originally in just the SELECT command, but I also added into the INSERT command in case I need to do something in the on_insert or on_inserting event. I'm not sure if that would actually work since I click the button to enter.

Anyway, if a duplicate key is entered in branch_id the error "ORA-00001: Unique constraint violated" is raised. I would like to change this and put my own error message, such as "ID is already used, select another." What I have so far is below. I do still get the same message.

I created a simple data access layer in the dll and reference it in my web app. How can I point the error to the user that his code in this line has an exception. currently, when i input a nonexisting table it will popup an error "invalid object name table_name" and points the my code throw exception inside my dll. I want it to point out to the user code that uses my function that creates an error.

When this code is executed when the Database server is being shutdown and the communication is disrupted, .NET throws the following error: Item has already been added. Key in dictionary: 'Query:' Key being added: 'Query:' at System.Collections.ListDictionaryInternal.Add(Object key, Object value) How can this be?

We are using renderaction provided by MVC futures. We've noticed that HttpExceptions in child action methods are not propagated to the parent action. Even though a fragment has failed, we still see the parent request resulting in a 200, and this throws our error handling for a toss.

I have a weird situation here. Using VS2010 and the MVC2 application template, I'm trying to set up error handling, but it refuses to work... Even with a simple controller throwing an exception in the index:

[Code]....

And <customErrors mode ="On"></customErrors> in the web.config file, MVC breaks into the code and tells me that "Exception was unhandled by user code". The default Error.aspx (static text only) is located in /Views/Shared, and I've even removed the login partial view from the master page.

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code

After banging my head because i could find where the exception came for and global.asax didn't get hit i found that it's from server.transfer and is a known bug. The alternative i read about is server.execute.However if i do this then the next page fires multiple javascript exceptions and a gridview i have is breaking of and the rows appear half at the left page corner and half inside the gridview!!

For now i just put a try catch on the server.transfer although the exception will not affect the application even if i don't use try catch. I also see that the exception is happening again even if i put try catch but with different dll: A first chance exception of type 'System.Threading.ThreadAbortException' occurred in App_Web_benglxq1.dll An exception of type 'System.Threading.ThreadAbortException' occurred in App_Web_benglxq1.dll but was not handled in user code Now what on earth is benglxq1.dll i don't know. What i know is that i'm leaving an exception to run and i don't like that. This is the js error when i use server.transfer btw: Microsoft JScript runtime error: Sys.ArgumentTypeException: Object of type 'Sys._Application' cannot be converted to type 'Sys._Application'.

I'm using ASP.NET Webforms and using stored procedures/ADO.NET for DataAccess. Now he does not want me to hard code validation in stored procedures nor in the code behind, but to catch the constraint exception message and look up the same error message in the database and look for the message that we want to show. I wonder if his design will work or should I explain to him that something is wrong here.

After many years reading this forum... I didn't found the solution to my actual problem... When I tought... this could be a big problem... I have a web app wich doesn't have try catch blocks in every method... so I need to implement a generic solution to handle exceptions and the most important... show some BO exceptions to user inside a MessageUserControl (ASCX) wich uses AJAX. this is the big problem, I tried many solutions, application_error, a specific log module, page_error... and with all I got to the same problem...

The UpdatePanel, when it got an unhandled exception, it fires the events (application_error, page_error) or even the httpmodule, but I think it call an Response.End, wich stops any action in the page... and this is just I don't want to do... I need to display the message to the client. this is the call to show the message...

[Code]....

My page wich gots an unhandled exception are inside an UpdatePanel, if I remove it, the message shows UP! I'm working on this for 5 long days.

i am having trouble catching errors in an AJAX panel. Even when i throw an exception in the c# code behind the front end completely ignores it. Here is the code i have setup, can anyone see why? I ideally want to show a js alert window on error.

I am going to be receiving data in a web service. After I receive this data I will be mapping it to a data transfer object. I want to be able to catch exceptions if the data from the service is missing any of the required values, times out or any data is malformed.

I'm currently working on a 3-tier ASP.NET application (UI, BLL & DAL). The DAL uses a strongly typed dataset that I've created with the VS Dataset Wizard. My question is, what is the best way to handle exceptions originating from the BLL and DAL classes. I googled a bit and it seems that the most commonly used practice is to create DALException and BLLException classes and throw your own message. Is this the way forward? how this can be done for an automatically generated DAL? What are the best practices?

I have introduced site wide exception handling on my site to catch unexpected errors. The event handler sends a message to me when such an exception occurs. But the problem is that any clicks on my site will cause an exception to be thrown with the message: "This is an invalid webresource request."

I googled for an answer and only found a couple of references to 1. setting a fixed machinekey (which for some reason didn't work for me, I got an error saying that the virtual directory wasn't set in IIS, and as far as I know I'm not using IIS at all, I'm using the local development environment and then deploy to a web host) or 2. using a robots.txt file to stop robots from accessing the axd files, but that doesn't help either because it's not robots that cause the problem, anyone clicking around on the page will cause this error. how to solve this problem properly? For now I have handled it by catching and ignoring this particular error by the text in the error message, which works but I would rather fix it so that this error doesn't occur at all.

I am using the standard createuser wizard (but got same issue with other wizard) and in the CreatedUser event an exception is raised causing the wizard not to reach the next step. If no exception are raised the wizard behave as expected. I have tried to force the wizard to move to the next step using CreateNewMember.MoveTo(this.CompleteWizardStep1); as an exemple but still the web page is in waiting stage for about 5 to 10 mins before it finally reach the final step. I have tried to deal with the exception in the routine it occured or throw it back to the CreatedUser event and deal with it there but nothing changes as of to the move to the next step in the wizard.

I am part of a team that is developing a SharePoint web part. My task is to create a user control which will ultimately be wrapped in another class for presentation in a web part. Because of these requirements, I must fit all of my code in a single user control.To organize my code in the code-behind, I created a few private classes for the different objects which are represented in the database. I want to add some exception handling whereby if some sort of DB exception is thrown I can update a message on the page informing the user of the problem.

My problem is that the page-level controls are inaccessible from the private class where I'm issuing the query, so even if I catch the exception I have no way of directly accessing a Label control to populate the error. I tried to create a baseclass with an EventHandler defined, but when I throw events from the private class I'm not able to catch them for whatever reason. I'm far from an OOP expert Here is a snippet:ASP Code:using System;using System.Collections;[code]....