一、线程异常
我们在单线程中,捕获异常可以使用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#集合查询Linq在项目中使用详解 2023-06-09
- c#读取XML多级子节点 2022-11-05
- C#通过GET/POST方式发送Http请求 2023-04-28
- C#中类与接口的区别讲解 2023-06-04
- C# Winform实现石头剪刀布游戏 2023-01-11
- Unity shader实现多光源漫反射以及阴影 2023-03-04
- 使用c#从分隔文本文件中插入SQL Server表中的批量数据 2023-11-24
- c#中利用Tu Share获取股票交易信息 2023-03-03
- 京东联盟C#接口测试示例分享 2022-12-02
- C# – NetUseAdd来自Windows Server 2008和IIS7上的NetApi32.dll 2023-09-20