C# 异常捕获

文章目录

C# 异常捕获

捕获异常

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Test
    {
        int result;
        Test()
        {
            result = 0;
        }
        public void division(int num1,int num2)
        {
            try
            {
                result = num1 / num2;
            }
            catch(DivideByZeroException e)
            {
                Console.WriteLine("Exception caught:{0}", e);
            }
            finally
            {
                Console.WriteLine("Result:{0}", result);
            }
        }
        static void Main(string[] args)
        {
            Test t = new Test();
            t.division(23, 0);
            Console.ReadKey();
        }
    }

}

运行效果

自定义异常

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class TempIsZeroException : ApplicationException
    {
        public TempIsZeroException(string message) : base(message)
        {
        }
    }
    public class Temperatrue
    {
        int temperature = 0;
        public void show()
        {
            if (temperature == 0)
            {
                throw (new TempIsZeroException("ZERO TEMPERATRUE FOUND"));
            }
            else
            {
                Console.WriteLine("Temperatrue:{0}", temperature);
            }
        }
    }
    class Test
    {
        int result;
        Test()
        {
            result = 0;
        }
        public void division(int num1,int num2)
        {
            try
            {
                result = num1 / num2;
            }
            catch(DivideByZeroException e)
            {
                Console.WriteLine("Exception caught:{0}", e);
            }
            finally
            {
                Console.WriteLine("Result:{0}", result);
            }
        }
        static void Main(string[] args)
        {
            Temperatrue t = new Temperatrue();

            try
            {
                t.show();
            }
            catch (TempIsZeroException e)
            {
                Console.WriteLine("TempIsZeroException:{0}", e.Message);
            }
            Console.ReadLine();
        }
    }

}

运行结果

抛出异常

csharp 复制代码
catch(Exception e)
{
   ...
   Throw e
}
相关推荐
c#上位机12 小时前
halcon图像增强——emphasize
图像处理·人工智能·计算机视觉·c#·上位机·halcon
zxy284722530114 小时前
C#的视觉库Halcon入门示例
c#·图像识别·halcon·机器视觉
c#上位机19 小时前
halcon区域变换—shape_trans
图像处理·算法·计算机视觉·c#·halcon
FuckPatience19 小时前
C# PropertyGrid(属性编辑框)利用DisplayAttribute实现多语言
c#
小哈龙21 小时前
c#安装nuget包--包源映射
c#·nuget
BuHuaX21 小时前
Lua入门
开发语言·unity·junit·c#·游戏引擎·lua
Sunsets_Red21 小时前
【算法日常】浅谈倍增(好吧我是用来凑字数的)
c语言·c++·学习·算法·数学建模·c#·学习方法
信道者1 天前
C#14在.NET 10中的更新
开发语言·c#
c#上位机1 天前
halcon图像非线性对比度增强——equ_histo_image
计算机视觉·c#·上位机·halcon·机器视觉
武藤一雄1 天前
Avalonia与WPF的差异及避坑指南 (持续更新)
前端·前端框架·c#·.net·wpf·avalonia