Block block selection

I am reading a C # article. This suggests that

At the end of the catch block, you have three options:

<i> <b> • Repeat the same exception by notifying the code above in the call stack for the exception.

• Throw another exception, providing more details on the exceptions for the code above on the call stack.

• Let the thread drop out of the bottom of the catch block.

I cannot understand the points. It would be of great help if you could clarify this by presenting a simple example.

Thanks in advance.

Update: When I need to re-handle the thrown exception, I need to have try .. catch nested files like

try
{
   try
   {
   }
   catch(InvalidOperationException exp)
   {
     throw;
   }

}
 catch(Exception ex)
 {
    // handle the exception thrown by inner catch block
   // (in this case the "throw"   clause     inside the inner "catch")
 }
}

      

+2


source to share


9 replies


Well, these are the different options in the code:

Option 1: Ritrow

try
{
    // Something
}
catch (IOException e)
{
    // Do some logging first
    throw;
}

      

Option 2: choose a different exception



try
{
    // Something
}
catch (IOException e)
{
    // Do some logging first
    throw new SorryDaveICantDoThatException("Oops", e);
}

      

Option 3: let the thread fall from the bottom

try
{
    // Something
}
catch (IOException e)
{
    // Possibly do some logging, and handle the problem.
    // No need to throw, I've handled it
}

      

EDIT: To answer the additional question, yes - if you need to handle a re-run exception that needs to be handled in the outer scope, exactly as shown in the question. This is very rare. Indeed, blocks catch

should be relatively rare, in the first place, and even more nested.

+6


source


It's a choice. The difference between 1 and 2 is that if an exception is thrown and you want to debug a position, it gets picked, you get option 1 there (all the way down in a try block on a specific object). With option 2, you will be up and up only on this line (run a new Exception2 ())

3 is when you want to ignore the exception and just keep going



//1
catch (Exception ex)
{
    throw;
}

//2
catch (Exception ex)
{
    throw new Exception2();
}

//3
catch (Exception ex)
{
}
return something;

      

+2


source


On most production systems, the last thing you want is a truly unhandled exception. This is why you usually try to catch statements.

You might be wondering why you want to throw the error you caught and here are some real world examples.

  • You caught the exception in your WCF application, logged the exception, and then threw a faultException to return to the WCF client. Similarly, you can have traditional asmx, catch the exception, and then throw the SOAP exception back to the client. The point of certain exceptions is to follow certain rules: a standard .net exception won't be well digested by a WCF client for example.

  • You caught the exception somewhere deep inside your code, logged the exception, and maybe even took some action. But above in your code you have another procedure that also waits for an exception, for now above, the exception can easily change the business process. By removing the exception below, the code above is not aware of any exception, so you need to throw the exception to be caught above, so the code you wrote there can customize the workflow. Ofc none of this magically happens, it all has to be coded and different programmers use different methods.

  • You may also want to get an exception from only one or a few statements, for example, get the configuration value from an XML file, if something goes wrong, .net might just return an object reference not set. You can catch this and then rebuild the exception as "Configuration value: no client name specified."

Hope it helps.

+2


source


1) Re-throw

try
{
    ...
}
catch (Exception e)
{
    ...
    throw;
}

      

2) Throw a new exception

try
{
    ...
}
catch (Exception e)
{
    ...
    throw new NewException("new exception", e);
}

      

3) Autumn

try
{
    ...
}
catch (Exception e)
{
    ...
}

      

+1


source


remove the same exception:

try
{
    // do something that raises an exception
}
catch (SomeException ex)
{
    // do something with ex
    throw;
}

      

introduce another exception

try
{
    // do something that raises an exception
}
catch (SomeException ex)
{
    // do something with ex
    throw new SomeOtherException(ex);  // NOTE: please keep ex as an inner exception
}

      

let the stream drop:

try
{
    // do something that raises an exception
}
catch (SomeException ex)
{
    // do something with ex
}
// the code will finish handling the exception and continue on here

      

+1


source


You can also go back to the catch block, so there is a 4th option. Returns false, returns null, etc. (Even returns the default.)

Preventing the catch block from falling implies that you have successfully dealt with the Exception that was raised in your try block. If you haven't, it's best to reverse or fail in some other way.

0


source


this is not a c # spec, it applies to any programming language (throw them exceptions, throw them errors, call them whatever you want).

So, the answer to your question is that this is a basic premise of all programming and you must determine the correct action to be taken in your code, given the error, circumstances, and requirements.

0


source


Taylor,

When you learn about handling Exception I would like to add my 2 cents. Throwing exceptions is very expensive (expensive, of course, due to a memory hack), so in this case you should consider assigning the error message to a string and transferring it through the application to a log or something.

eg:

string errorMessage = string.empty;

try
{
...
}
catch(Exception e)
{
   errorMessage = e.Message + e.StackTrace;;
}

      

So you can carry that string anyway. This string can be a global property and can be emailed or registered in a text file.

0


source


I think there is something to add to the great answers we already have.

This may or may not be part of your overall architectural design, but what I've always observed is that you usually only catch where you can add value (or recover from an error) - in other words, don't try .. .catch ... repeat several times for one operation.

Typically you should plan for a consistent pattern or design for how you handle exceptions as part of your overall design. You should always plan on handling exceptions anyway, unhandled exceptions are ugly!

0


source







All Articles