C# exception handling

        Although I have known about the existence of "try...catch...finally..." in C#, I have never used it. It may be subconsciously "If an exception occurs, it means that the program has a bug. Shouldn't you go to debug? Why do you want this thing?" I think, but I found out yesterday that there are always some situations that you can't imagine QAQ. In order to ensure that even if there is an abnormality, it can still be dealt with correctly, this part of the grammar is still very useful.

    try...catch...finally is a structured exception handling syntax included in the C# language. The basic structure is

            try
            {

            }
            catch(<ExceptionType>)
            {

            }
            finally
            {

            }

    The curly braces after try contain code that might throw an exception.

    catch can specify Exception Type, only respond to specific exceptions, otherwise respond to all exception types by default.

    finally is the code that will be executed no matter what, if there is no exception, it will be executed after try, and if there is an exception, it will be executed after catch.

    In a try...catch...finally, the structure is very flexible:

    try+finally,

    try+catch,

    try+catch+finally,

    try+catch(Exception 1)+catch(Exception 2)+……,

    try+catch(Exception 1)+catch(Exception 2)+……+finally.

    In addition, try...catch...finally can be nested.

    The order of execution of try...catch...finally is:

    If there is no exception during the execution of try, execute finally (if there is one).

    If an exception occurs during the execution of try, it will be interrupted where the exception occurs.

        If there is a catch, compare the catches one by one to see if there is a match with the thrown exception.

            If there is a match for the thrown exception, execute the corresponding method, then execute finally (if there is one).

            If not, execute finally (if it exists).

        If there is no catch, execute finally (no catch must have finally).

In the current test task, the problem encountered is that when the test system is abnormal, the measured data is not recorded normally, resulting in a large number of unnecessary repeated tests and wasting time. Below is a test function that writes data to the ExceptionTest.txt file. When i=11, the array size will be exceeded and an exception will occur.

Without exception handling, the data is not successfully written to the file after an exception occurs and is interrupted.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace CSLearning
{
    public class ExceptrionLearning
    {
        public static void OutIndexExceptionTest()
        {
            string testpath = @"C:\Users\songmei1\Desktop\ExceptionTest.txt";
            StreamWriter mswriter = new StreamWriter(testpath, false, Encoding.UTF8);
            int [] testArray = new int [10];          
            for (int i = 0; i < 11; i++)
            {
                testArray [i] = i;                    
                mswriter.WriteLine(testArray[i].ToString());                    
            }
            mswriter.Close();
            mswriter.Dispose();
            mswriter = null;
        }
    }
}

After adding the exception handling structure, regardless of whether an exception occurs, StreamWriter will execute finally and shut down normally, and the data will be successfully written.

            try
            {
                for (int i = 0; i < 11; i++)
                {
                    testArray [i] = i;                    
                    mswriter.WriteLine(testArray[i].ToString());                    
                }
            }
            catch(Exception)
            {
                mswriter.WriteLine("Exception occured!");
            }          
            finally
            {
                mswriter.Close();
                mswriter.Dispose();
                mswriter = null;
            }

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325653304&siteId=291194637