[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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...
0
ASPDEV
Asked:
ASPDEV
  • 2
  • 2
  • 2
  • +1
1 Solution
 
käµfm³d 👽Commented:
That example doesn't make much sense. Why would you wrap integer comparison in a try/catch?
0
 
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
 
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now