Deep C# - Take Exception to Everything
Written by Mike James   
Thursday, 12 March 2020
Article Index
Deep C# - Take Exception to Everything
Catch Clauses
What is an Exception
The State Problem
ThreadException handling

Structured exception handling sounds like a really good idea until you start to look at it more closely - then it almost becomes worse than the problem it is trying to solve. We look at exception handling - the philosophy, the strategy and the way C# does the job.

Deep C#

 Buy Now From Amazon

DeepCsharp360

 Chapter List

  1. Why C#?

    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing ***NEW!
  3. Value & Reference
  4. Structs & Classes
  5. Inheritance
  6. Interfaces & Multiple Inheritance
  7. Controlling Inheritance

    II Casting & Generics
  8. Casting - The Escape From Strong Typing
  9. Generics
  10. Advanced Generics
  11. Anonymous & Dynamic

    Typing
    III Functions
  12. Delegates
  13. Multicast Delegates
  14. Anonymous Methods, Lambdas & Closures

    IV Async
  15. Threading, Tasks & Locking
  16. The Invoke Pattern
  17. Async Await
  18. The Parallel For

    V Data - LINQ, XML & Regular Expressions
  19. The LINQ Principle
  20. XML
  21. LINQ To XML
  22. Regular Expressions

    VI Unsafe & Interop
  23. Interop
  24. COM
  25. Custom Attributes
  26. Bit Manipulation
  27. Advanced Structs
  28. Pointers 

 <ASIN:1871962714>

 <ASIN:B09FTLPTP9>

Once upon a time we wrote programs that worked and they were designed not to crash.

Of course this is a fairy story and what we actually did was to write programs that worked as long as nothing unusual happened - that is as long as the input and operating conditions were as "expected" by the programmer.

If this wasn’t the case well - what could you expect - the program was broken and beyond our control. The runtime system generally ended the program with an "abnormal end" or "abend".

Of course today this would be a ridiculous way to handle errors but programs still fail at runtime because eliminating runtime errors is still usually something of an afterthought. 

The solution to the problem is generally accepted to be structured exception handling - that is try-catch to you and me. However the whole subject is trickier than you might imagine.

Let's take a look at how it all works and how it all might be made to work.

Try-catch

The basic syntax and operation of exception handling is very simple.

You surround any block of code that you think might fail with a try clause. If something does go wrong the block of code generates, or throws, an exception which is caught by the catch clause.

For example:

int a=10;
int b=0;
try
{
int result = a / b;
}
catch
{
MessageBox.Show("division by zero isn't allowed");
}

In this case it is clear that b is zero and so a/b cannot be computed and so the CLR throws a runtime exception. This is caught by the catch clause and the message is displayed.

This is a very common way to use the try-catch but it has a problem.

Suppose the exception is caused by something other than a divide by zero error. The catch clause operates no matter what the problem is with the try block leading the user and the programmer trying to fix the problem to believe that it is a division by zero problem. The solution is to always specify what exception you are handling with a conditional catch clause.

For example:

catch(DivideByZeroException)
{
MessageBox.Show("division by zero isn't allowed");
}

Notice that the catch clause now only handles exceptions of type DivideByZeroException.

csharp



Last Updated ( Thursday, 12 March 2020 )