Click here to Skip to main content
14,733,966 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When it comes to exception handling in a .net application, does one need to develop a custom class to handle all exceptions for the application or there exist an inbuilt class that can be used for exception handling in the application. Furthermore, can errors such as empty strings, only alphabets etc be handled by some inbuilt functionality in .net or do i need to include this also in a custom class.

I am asking because i have written the code below and the compiler seems to skip the catch blocks when i run the program

try
{
    conn.Open();
    dept = deptName.Text;
    cmd.Parameters.Add(new SqlParameter("dept", dept));
    cmd.ExecuteNonQuery();
    MessageBox.Show("The department has been created.", "Task Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (ArgumentNullException)
{
    MessageBox.Show("empty text box");
}
catch (FormatException)
{
    MessageBox.Show("Please enter only alphanumeric characters", "Input Problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
Posted
Updated 26-Jan-11 7:47am
v2
Comments
Nish Nishant 26-Jan-11 13:18pm
   
Updated my reply based on your updated question.

There are several Exception derived class built into the framework that you can use for specific purposes. Examples are ArgumentException, InvalidOperationException etc.

For errors specific to your application it's usually a good idea to have your app-specific exception type. Most big apps will probably have their base exception type and then more specific derived classes for more specific errors and exceptions. One pattern/approach I've observed is that sometimes when an unknown exception is thrown, and there's no handler for it it's caught by the application and re-thrown as an app-specific exception with the original exception specified as the InnerException.

[Update]
----------

This is in response to your updated question. If it skips all three catch blocks, it's likely that there is no exception being thrown.
   
v3
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 15:08pm
   
That's a good answer.
--SA
fjdiewornncalwe 26-Jan-11 17:14pm
   
Exactly.... +5
The base class of all the exceptions is Exception. When you create your own exception class, you would need to derive from Exception or one of its derived classes.

When you handling exceptions, it gets processed sequentially. Thus you need to handle it the way you you have shown

catch (ArgumentNullException)
{
    MessageBox.Show("empty text box");
}
catch (FormatException)
{
    MessageBox.Show("Please enter only alphanumeric characters", "Input Problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}


The top most class needs to be the last in your catch block. Otherwise, the derived class below it won't be visible. The way you have the code is ok, and since (Exception ex) is your last catch block, as it should be, you don't have any exception at all. If you had any no matter of any exception type, it would have been caught by your last catch block.

So, your source of problem may not be exception.
   
Give this a try. It should catch all exceptions:
try
{
    // Do some stuff.
}
catch
{
    MessageBox.Show("an unknown exception was caught.");
}

Could be that you are experiencing a timeout, so you are stopping the debug session before the exception gets a chance to be thrown.
   
Comments
AspDotNetDev 26-Jan-11 13:21pm
   
Also, but a messagebox in a finally block. That way, you can be sure you are not just experiencing a timeout.
Exception is the parent class of all exception classes (.NET Class Library classes and your own classes). So if an exception ocurrs, it will be caught in one of your catch blocks. If not, it means that there were no exceptions thrown.

BTW, you shouldn't be calling GUI code (MessageBox.Show) in DAL (Data Access Layer). You should throw .NET Framework exception or your own exception type to the invoker. Eventually, the GUI will get and exception and it will call MessageBox.Show. Summary:
* DAL: exception occurs. You can throw .NET exception or wrap .NET exception with your own exception and throw that exception
* GUI: You catch the exception and call MessageBox.Show

Another key points:
* Always close connections to DB
* Use try-catch-finally or using keyword to make sure the connection gets closed not matter what happend (executes successfully or not).
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900