一、线程异常
我们在单线程中,捕获异常可以使用try-catch,代码如下所示:
using System;
namespace MultithreadingOption
{
class Program
{
static void Main(string[] args)
{
#region 单线程中捕获异常
try
{
int[] array = { 1, 23, 61, 678, 23, 45 };
Console.WriteLine(array[6]);
}
catch (Exception ex)
{
Console.WriteLine($"message:{ex.Message}");
}
#endregion
Console.ReadKey();
}
}
}程序运行结果:

那么在多线程中如何捕获异常呢?是不是也可以使用try-catch进行捕获?我们先看下面的代码:
using System;
using System.Threading.Tasks;
namespace MultithreadingOption
{
class Program
{
static void Main(string[] args)
{
#region 单线程中捕获异常
//try
//{
// int[] array = { 1, 23, 61, 678, 23, 45 };
// Console.WriteLine(array[6]);
/
沃梦达教程
本文标题为:C#多线程的相关操作讲解
基础教程推荐
猜你喜欢
- C#中参数的传递方式详解 2023-06-27
- C#执行EXE文件与输出消息的提取操作 2023-04-14
- C#使用Chart绘制曲线 2023-05-22
- Unity虚拟摇杆的实现方法 2023-02-16
- 如何用C#创建用户自定义异常浅析 2023-04-21
- C#使用NPOI将excel导入到list的方法 2023-05-22
- C#使用SQL DataAdapter数据适配代码实例 2023-01-06
- 浅谈C# 构造方法(函数) 2023-03-03
- C# TreeView从数据库绑定数据的示例 2023-04-09
- C#实现归并排序 2023-05-31
