mjsr
mjsr

Reputation: 7590

what is the correct ways of use try-catch block in C#

i would like to know your opinions of what is the correct way of applying the try-catch block to capture exceptions.

Let say i have 4 levels of hierarchy methods that are calls like this:

method1(){
   method2(){
       method3(){
          method4()
       }
       morecode that depend of what happend in method3
   }
   morecode that depend of what happend in method2
}

So what i do is wrap from inside out the possible methods that are going to present exceptions and as i have code that depends in the different levels of what happen in those methods i propagate the exception using "throw" sentences to avoid that those codes produce a crash.

method1(){
   try
   method2(){
       try
       method3(){
          try
          method4()
          catch
          throw
       }
       catch
       throw
       morecode that depend of what happend in method3
   }
   catch
   return
   morecode that depend of what happend in method2
}

Is this the correct way? or i'm making a terrible use of "throw" sentence?

Upvotes: 1

Views: 966

Answers (7)

Dekker500
Dekker500

Reputation: 821

You should catch exceptions at the first level where you want to actually DO something about it.

No need to have multiple try-catch blocks if the end result is the same "generic" action. Just use one catch and handle the errors.

If, however, a catch block changes the result, such that a calling method can react differently, then it should be embedded.

Exception handling is a rather convoluted but important topic...

Suggested title on topic: Robust ASP.Net Exception Handling

Upvotes: 4

tanasi
tanasi

Reputation: 1804

You can try something like this:

        try
        {
            // Your code
        }
        catch (Exception ex)
        {
            switch (ex.Message)
            {
                case "Some system exception information":                       
                    MessageBox.Show("Your text to replace system exception information",
                     "Warning",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning);  
                    break;      
                default:
                    MessageBox.Show(ex.Message,
                    "Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);                       
                    break;
             }

         }

Upvotes: 0

Security Hound
Security Hound

Reputation: 2547

You only have to use a single try method, considering if anything fails within the try statement, then the catch statement will happen. You don't want to go throwing exceptions, you want to attempt to handle them, and only throw exceptions when an event happens that you cannot handle.

Design your application in a way so that you can avoid a design like the one you posted.

Upvotes: 0

petro.sidlovskyy
petro.sidlovskyy

Reputation: 5103

Catch exception only in case you know how to handle it. Other practice is to rethrow new exception by setting original one as inner exception. In some cases it helps to handle exceptions better.

Upvotes: 0

Lee Baker
Lee Baker

Reputation: 572

The try/catch blocks further down in your call hirachy are only necessary when you have something that needs to be cleaned up in an finally block. If you are just going to throw the exception again you are adding code that is not needed.

You could also use a Using block lower down if the objects implement IDisposable.

Upvotes: 0

Oded
Oded

Reputation: 499392

If all you are going to do in your catch is re-throw, don't bother with the try at all.

Exceptions will bubble up, so there is no need to catch and re-throw like that - you will still get the correct stack trace.

Upvotes: 6

leppie
leppie

Reputation: 117360

(Personally) I would try workaround every 'exceptional' case, and then only litter the code with try/catch blocks where needed.

Upvotes: 1

Related Questions