异常捕获及循环结构
1、异常捕获
C#异常捕获和JAVA相同,语法如下:
try
{
可能会出现异常代码
}
catch
{
出现异常后执行的代码
}
------------------------------------------------------
examples:
namespace 异常捕获
{
class Program
{
static void Main(string[] args)
{
int num = 0;
bool b = true;
try
{
num = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的必须是数字");
b = false;
}
if (b == true)
{
Console.WriteLine(num * 2);
}
Console.ReadKey();
}
}
}
-----------------------------------------------