01. Exception Capture

Source: Internet
Author: User

What is an exception? Explanation: There is no grammatical error, in the process of running the program, hesitate for some reason, the program has been wrong, can no longer run normally.
We often have a variety of anomalies in the program, if you want the program to be strong, I should be regular use of Try-catch for exception capture.
Tip: Which line of code is likely to be an exception, let's try It
Syntax: try{code may appear exception;} catch{code to execute after an exception occurs;}

Execution procedure: If the code in the try does not have an exception, the code in the catch will not be executed. If the code in the try has an exception, the code after the exception appears will no longer execute, but will jump to the catch to execute the code.
Scope of the variable: the scope of the variable is the extent to which you can use the variable. The scope of a variable is typically started with the parenthesis that declares it, and the parentheses that correspond to that parenthesis end. In this range, we can use this variable beyond this range and no longer have access to the variable.
  
 
  1. namespace _01.异常捕获
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //什么是异常?
  8. //异常就是语法上代码没有任何问题,但是运行中因为各种各样的原因,而出现的问题的情况,
  9. //被称之为异常.
  10. //示例:
  11. Console.WriteLine("请输入一串数字:");
  12. int number = 0; //声明一个int类型的变量并赋初值.
  13. try
  14. {
  15. number = int.Parse(Console.ReadLine()); //用于接收数字,但是用户也可能会输入其它字符,这个时候就会出现异常
  16. }
  17. catch
  18. {
  19. Console.WriteLine("输入的内容不能转换成数字.");
  20. }
  21. Console.WriteLine("数字:{0}.",number);
  22. Console.ReadKey();
  23. }
  24. }
  25. }


Change the code a little bit:
  
 
  1. namespace _01.异常捕获
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //什么是异常?
  8. //异常就是语法上代码没有任何问题,但是运行中因为各种各样的原因,而出现的问题的情况,
  9. //被称之为异常.
  10. //示例:
  11. bool b = true;
  12. int number = 0; //声明一个int类型的变量并赋初值.
  13. Console.WriteLine("请输入一串数字:");
  14. try
  15. {
  16. number = int.Parse(Console.ReadLine()); //用于接收数字,但是用户也可能会输入其它字符,这个时候就会出现异常
  17. }
  18. catch
  19. {
  20. Console.WriteLine("输入的内容不能转换成数字.");
  21. b = false;
  22. }
  23. //我们如果需要执行下面的这行代码,需要满足某些条件.
  24. //让代码满足某些条件去执行的话,使用bool类型.
  25. if (b == true)
  26. {
  27. Console.WriteLine("数字:{0}.", number);
  28. }
  29. Console.ReadKey();
  30. }
  31. }
  32. }





From for notes (Wiz)

01. Exception Capture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.