[C # Basics] The most common pen questions about try... catch

Source: Internet
Author: User

Introduction in the summary of the common interview questions, there are still a lot of methods to handle try... catch exceptions. Today, I will learn this interview question again. Try... catch syntax the try-catch statement consists of a try block followed by one or more catch clauses, which define different exception handling programs. When an exception is thrown, the Common Language Runtime (CLR) searches for the catch statement that handles the exception. If the currently executed method does not contain such catch blocks, the CLR will view the method that calls the current method, and then traverse the iterator to use the stack. If the catch Block is not found, the CLR displays the user with a one-day message about the unhandled exception and stops the program. The try block contains protection code that may cause exceptions. This block is executed until an exception is thrown or the block is successfully completed. Copy code 1 class Program 2 {3 static void Main (string [] args) 4 {5 object o = null; 6 try 7 {8 // NullReferenceException caused by an attempt to forcibly convert null objects 9 int I = (int) o; 10} 11 12 catch (NullReferenceException ex) 13 // 1. although catch clauses without parameters can be used to catch exceptions of any type, this method is not recommended. Generally, you should only capture exceptions that you know how to recover from. Therefore, you should always specify an object parameter derived from System. Exception. 14 // 2. You can use more than one specific catch clause in the same try-catch statement. In this case, the order of the catch clause is very important because the catch clause is checked in order. Exceptions with a higher level will be captured first, rather than exceptions with a lower level. If you sort catch blocks so that they cannot reach the subsequent blocks, the compiler will generate an error. 15 // 3. int I = (int) o; it is very likely that NullReferenceException is thrown, so put this catch in front of 16 // 4. if you place catch (Exception ex) in the first one, the subsequent block will not be reached, and the compiler will generate an error. 17 {18 Console. writeLine (ex. message); 19} 20 catch (DivideByZeroException div) 21 {22 Console. writeLine (div. message); 23} 24 catch (Exception ex) 25 {26 Console. writeLine (ex. message); 27} 28 Console. read (); 29} copy the basic syntax of the try-catch code as described above. The execution order of try-catch is: if an exception occurs during code execution in the try block, the code in the catch block will be executed, if the execution in the try block is correct, the code in the try block is executed completely and the statements in the catch Block are not executed. Try-catch-finally: the code in the finally block is executed no matter whether an exception exists or not, and the finally block is often used for the release of resources. Try-catch: Copy code of the Value Type 1 static int GetResult () 2 {3 int a = 1; 4 int B = 2; 5 int n = 1; 6 try 7 {8 int k = a/B; 9 return n; 10} 11 catch (Exception ex) 12 {13 14 Console. writeLine (ex. message); 15 throw; 16} 17 finally // If any exception occurs, finally will execute 18 {19 n ++; 20} 21} to copy the code. You may already know the result: 1. Why? Let's take a look at the decompiled code and you will know why. Type 2: Copy code of reference type 1 static Person GetResult () 2 {3 int a = 1; 4 int B = 2; 5 Person p = new Person (); 6 p. age = 1; 7 try 8 {9 int k = a/B; 10 return p; 11} 12 catch (Exception ex) 13 {14 15 Console. writeLine (ex. message); 16 throw; 17} 18 finally // If any exception occurs, finally executes 19 {20 p. age ++; 21} 22}

Related Article

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.