Synchronization of old projects
Part1
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
namespace Lesson8
|
||||
{
|
||||
class program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("异常捕获");
|
||||
//作用:解决程序运行时的异常问题,避免卡死或崩溃
|
||||
|
||||
#region 语法
|
||||
//基本语法:try{}catch{}尝试执行代码块中的代码,如果没有异常则继续执行
|
||||
try
|
||||
{
|
||||
//可能会发生异常的代码块
|
||||
}
|
||||
catch(Exception e)//(Exception e)是可选项,e是异常对象,可以获取异常信息
|
||||
{
|
||||
//捕获异常的代码块
|
||||
}
|
||||
//可选部分:finally{}在try或catch执行完后执行的代码块,无论是否有异常都会执行
|
||||
finally
|
||||
{
|
||||
//清理资源或执行收尾工作
|
||||
}
|
||||
//注意:异常捕获代码基本结构中,不需要加; 只有里面的代码需要加;
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 示例/实践
|
||||
//示例:除数不能为0的异常捕获
|
||||
try
|
||||
{
|
||||
Console.WriteLine("请输入一个整数:");
|
||||
string str = Console.ReadLine();
|
||||
int i = int.Parse(str);
|
||||
Console.WriteLine("你输入了:" + i);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("只能输入整数,请重新输入!");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.WriteLine("程序执行完毕。");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user