This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21123|Reply: 0

[Source] Exceptions and Exception Handling (C# Programming Guide)

[Copy link]
Posted on 4/12/2015 10:22:03 PM | | |

The exception handling feature of the C# language helps you handle any unexpected or unusual situations that occur while your program is running. Exception handling uses the try, catch, and finally keywords to try certain operations to handle failures, and although these operations have the potential to fail, you can try to do so if you are sure you need to do this and want to clean up resources afterwards. Common Language Runtime (CLR), . NET Framework or any third-party library or application code can generate exceptions. Exceptions are created using the throw keyword.
In many cases, exceptions may not be raised by a method called directly by code, but by another method further down the call stack. In this case, the CLR expands the stack to see if there is a method that contains a catch block for that particular exception type, and if it finds such a method, it executes the first such catch block found. If no appropriate catch block is found anywhere in the call stack, the process is terminated and a message is displayed to the user.
In this example, a method is used to detect if there is a situation where it is divided by zero; If there is, the error is caught. If there is no exception handling, this program will terminate and produce a "DivideByZeroException Not Handled" error.
  1. class ExceptionTest
  2. {
  3.     static double SafeDivision(double x, double y)
  4.     {
  5.         if (y == 0)
  6.             throw new System.DivideByZeroException();
  7.         return x / y;
  8.     }
  9.     static void Main()
  10.     {
  11.         // Input for test purposes. Change the values to see
  12.         // exception handling behavior.
  13.         double a = 98, b = 0;
  14.         double result = 0;

  15.         try
  16.         {
  17.             result = SafeDivision(a, b);
  18.             Console.WriteLine("{0} divided by {1} = {2}", a, b, result);
  19.         }
  20.         catch (DivideByZeroException e)
  21.         {
  22.             Console.WriteLine("Attempted divide by zero.");
  23.         }
  24.     }
  25. }
Copy code






Previous:C# determines whether a string contains a character
Next:Determine if a website is made by the Discuz forum program
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com