C# 异常捕获

C# 异常捕获

捕获异常

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();
        }
    }

}

运行效果

在这里插入图片描述

自定义异常

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();
        }
    }

}

运行结果

在这里插入图片描述

抛出异常

catch(Exception e)
{
   ...
   Throw e
}

相关推荐

  1. C# —— 异常捕获

    2024-03-22 11:50:02       8 阅读
  2. SpringBoot全局异常捕获

    2024-03-22 11:50:02       34 阅读
  3. CompletableFuture 异常捕获方式

    2024-03-22 11:50:02       9 阅读
  4. C++ 多线程中捕捉异常

    2024-03-22 11:50:02       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 11:50:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 11:50:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 11:50:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 11:50:02       18 阅读

热门阅读

  1. 深入理解浏览器的页面渲染机制

    2024-03-22 11:50:02       20 阅读
  2. 【wpf 应用6】基本控件-Label的详解与示例

    2024-03-22 11:50:02       20 阅读
  3. 光模块概述

    2024-03-22 11:50:02       20 阅读
  4. ngnix负载均衡

    2024-03-22 11:50:02       17 阅读
  5. 40 道高频 C++ 面试、笔试题及答案

    2024-03-22 11:50:02       18 阅读
  6. QT编程实现播放器(一)ffmpeg库的编译

    2024-03-22 11:50:02       17 阅读
  7. 卸载.Net SDK

    2024-03-22 11:50:02       18 阅读
  8. SpringCloud-Gateway源码笔记整理

    2024-03-22 11:50:02       22 阅读
  9. Gateway路由谓词(断言)功能

    2024-03-22 11:50:02       17 阅读
  10. 蓝桥杯 / 卡牌 /c\c++

    2024-03-22 11:50:02       20 阅读
  11. Pytorch 中的forward 函数内部原理

    2024-03-22 11:50:02       15 阅读
  12. 全志R128 SDK HAL 模块开发指南——CCU

    2024-03-22 11:50:02       15 阅读