Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Catching EXCEPTIONS with custom messages in C#

You can write your own custom error message instead of allowing the compiler's error message.

Example:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ForgetCode
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int top = 0, bottom = 0, result = 0;
  13. try
  14. {
  15. result = top / bottom;
  16. }
  17. catch (System.DivideByZeroException ex)
  18. {
  19. // Custom message for easy understanding
  20.  
  21. Console.WriteLine("You have attempted to divide a number with zero");
  22. }
  23. }
  24. }
  25. }

Output:
  1. You have attempted to divide a number with zero