C # exception caught

One, the background:

Because of poor basic programming, so recently begun to reinforce learning C # based on their own learning what the late, summed up in the article, there are deficiencies at Please exhibitions.

Second, Introduction

We often occur in a variety of abnormalities in the program, if you want your program more stability, should always use try-cath in your code to capture abnormal.

Which line of code possible exception occurs, we will use the try-cath.

Third, the syntax is:

try

{

Abnormal code may occur;

..........

.........

}

cath

{

After an exception code to be executed;

}

Implementation process : If you try the code is not abnormal, then the cath code will not be executed. If you try the code is abnormal, but jump directly to the cath execute code.

Fourth, examples

Find a value of twice the number

            bool b = true;
            int number = 0; // declare a variable
            Console.WriteLine ( "Please enter your number");
            try
            {
              number = Convert.ToInt32(Console.ReadLine()); //赋值
              
            }
            catch 
            {

                Console.WriteLine ( "what you can not convert digital input");
                b = false;
            }
            if (b)
            {
                Console.WriteLine(number * 2); //使用
            }
            
            Console.ReadKey()

Output

 

 

Guess you like

Origin www.cnblogs.com/qy1234/p/11772809.html