一、线程异常
我们在单线程中,捕获异常可以使用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# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C# 解析XML和反序列化的示例 2023-04-14
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中的Linq to JSON操作详解 2023-06-08
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity shader实现高斯模糊效果 2023-01-16
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#中 Json 序列化去掉null值的方法 2022-11-18