Try-Catch within Catch Exception handler C# .NET2005

Hello Experts,

Is it a god Practice to this:
 
I tried this:

catch (System.Data.SqlClient.SqlException sqlEx)
{

try
{
if (sqlEx.Class == 14 && sqlEx.Number == 2627)
    {
        //Duplicate Record
        return 1;
    }

     throw sqlEx;
}
catch (Exception ex) {
 }

}

It works, is it a good way of programming....

Open in new window


Since, I cannot use finally here.Because it will execute always...
ASPDEVAsked:
Who is Participating?
 
käµfm³d 👽Commented:
If you did something like this, then I would expect a try/catch to be necessary:
catch (System.Data.SqlClient.SqlException sqlEx)
{
    try {
        object code = "hello world";
        
        if (sqlEx.Class == 14 && sqlEx.Number == (int)code) // This will raise a new exception when you attempt to cast
        {
            //Duplicate Record
            return 1;
        }

        throw sqlEx;
    }
    catch (Exception ex) {
    }
}

Open in new window

0
 
käµfm³d 👽Commented:
That example doesn't make much sense. Why would you wrap integer comparison in a try/catch?
0
 
edemcsCommented:
No, it is not a good programming practice.  Try/Catches can be costly and you want to use them sparingly.  Instead, use a conditional statement.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AndyAinscowFreelance programmer / ConsultantCommented:
It works - are you certain?

try
{
if (sqlEx.Class == 14 && sqlEx.Number == 2627)
    {
        //Duplicate Record
        return 1;
    }

     throw sqlEx;   <<------------------ A
}
catch (Exception ex) {  <<---------------- B
 }


I am not 100% certain but I would expect the line A to be caught by B, if so then I don't think that is what you wanted.
0
 
ASPDEVAuthor Commented:
Below is the exact code:
Public int Test()
{ 

try
{
..............

return 1;
}

catch (System.Data.SqlClient.SqlException sqlEx)
{

try
{
if (sqlEx.Class == 14 && sqlEx.Number == 2627)
    {
        //Duplicate Record
        return 1;
    }

     throw sqlEx;
}
catch (Exception ex)
 {
   return 0;
 }

}
}

Open in new window



I want to catch an exception, if it is an SQLException(like duplicate Insert), I return as value 1. If it is other exception I " throw" the exception and catch in the another exception Block.


My point here is "Having Try-Catch withing Catch statement" is a good practice to do.  
0
 
ASPDEVAuthor Commented:
Thanks.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I've got around to testing.
My comment #35435274 is relevant.
What you are doing does NOT work the way you expect it to do, what you have accepted as an answer does not cure your original problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.