Exception statement Try Catch finally
try//protection execution inside the code snippet, if there is an error in one sentence, jump directly to the catch, will not control the following content
{
Console.Write ("Please enter an integer");
int a = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Hello");
}
Exception found in Catch//try, executed directly, if no error in try, do not execute
{
Console.WriteLine ("Wrong input! ");
}
finally//no matter if there is any mistake, all need to execute!
{
Console.WriteLine ("Thanks for using, goodbye!") ");
}
Console.ReadLine ();
For Repeating Loop:
Case:
for(inti =1; I <=3; i++) { if(i = =1) {Console.Write ("Do you love me or not? "); } if(i = =2) {Console.Write ("I ask you, do you love me or not? "); } if(i = =3) {Console.Write ("Are you deaf? I ask you love do not love me!! "); } stringA =Console.ReadLine (); if(A = ="Love") {Console.WriteLine ("Well, I love you, too. "); System.Threading.Thread.Sleep ( -);//Sleep 2 secondsConsole.WriteLine ("from then on the prince and the princess had a shameless life! "); Break; } Else { if(i = =3) {Console.WriteLine ("Get outta here! "); }}} console.readline ();
Enter month and date, determine whether the format is correct, if not correct, please re-enter:
Console.Write ("Please enter the year:"); for (; ; ) { inty =int. Parse (Console.ReadLine ()); if(Y >=0&& y <=9999) {Console.Write ("Please enter the month:"); intm =int. Parse (Console.ReadLine ()); for (; ; ) { if(M >=1&& m <= A) {Console.Write ("Please enter the date:"); for (; ; ) { intD =int. Parse (Console.ReadLine ()); if(d >=1&& D <= to) { if(M = =1|| m = =3|| m = =5|| m = =7|| m = =8|| m = =Ten|| m = = A) {Console.WriteLine ("input is correct! The date you entered is"+ y +"years"+ M +"Month"+ D +"Day."); Break; } Else if(M = =4|| m = =6|| m = =9|| m = = One) { if(d <= -) {Console.WriteLine ("input is correct! The date you entered is"+ y +"years"+ M +"Month"+ D +"Day."); Break; } Else{Console.WriteLine ("the date you entered is malformed, please re-enter:"); } } Else { if(Y% -==0|| Y%4==0&& y% -!=0) { if(d <= in) {Console.WriteLine ("input is correct! The date you entered is"+ y +"years"+ M +"Month"+ D +"Day."); Break; } Else{Console.WriteLine ("the date you entered is malformed, please re-enter:"); } } Else { if(d <= -) {Console.WriteLine ("input is correct! The date you entered is"+ y +"years"+ M +"Month"+ D +"Day."); Break; } Else{Console.WriteLine ("the date you entered is malformed, please re-enter:"); } } } } Else{Console.WriteLine ("the date you entered is malformed, please re-enter:"); } } Break; } Else{Console.WriteLine ("The month format you entered is incorrect, please re-enter:"); } } Break; } Else{Console.WriteLine ("the year you entered was incorrectly formatted, please retype it:"); }} console.readline ();
3.11 Lessons The exception statement repeats with the For loop