C# try catch

1、代码放到try快中(try是c#的关键字)。代码运行是,会尝试执行try块内部的语句,如果么有语句发生异常,这些语句将顺序执行下去。直到全部都完成,但是一旦出现异常就跳出try块,执行catch块中的内容。2、try块需要一个或者多个catch块程序捕捉并处理特定类型的异常。

实验步骤:首先通过控制台程序输入一串字符,使用Console.readLine();获取一串字符串数据。

然后使用后int.parse(string s);这个函数将字符串转换为int型数据。

通过查看int.parse(string s);函数定义可以知道他又如下异常。在这里插入代码片

// 异常:
        //   T:System.ArgumentNullException:
        //     s 为 null。
        //
        //   T:System.FormatException:
        //     s 的格式不正确。
        //
        //   T:System.OverflowException:
        //     s 表示一个小于 System.Int32.MinValue 或大于 System.Int32.MaxValue 的数字。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tesetData
{
    class Program
    {
       

       
        static void Main(string[] args)
        {
            //try catch的使用 
            string readString = Console.ReadLine();
            int readValue;
            try
            {
                readValue = int.Parse(readString);
                Console.WriteLine(readValue);
            }
            catch (OverflowException)
            {
                Console.WriteLine("err:转化的不是一个int型数据");
            }
            catch (FormatException)
            {
                Console.WriteLine("err:格式错误");
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("err:null");
            }
            Console.ReadLine();
        }
    }
}

异常过滤器:

异常过滤器是c# 6的新功能,它影响异常和catch处理程序的匹配方式,允许指定catch除了程序的额外,处理条件。这些条件采用的形式是when关键字布尔表达式

例如:

catch (OverflowException oe) when (oe.GetType() != typeof(System.FormatException))
{
//处理之前没有捕捉除了FormatException之外的所有异常
Console.WriteLine("FormatException之外的所有异常");
}
C#中,try-catch是一种错误处理机制。当程序在运行过程中出现异常时,使用try-catch可以捕获并处理这些异常,以避程序的崩溃或产生不可预料的结果。 try-catch语句的基本用法如下: ``` try { // 可能出现异常的代码 } catch (异常类型1) { // 处理异常类型1的代码 } catch (异常类型2) { // 处理异常类型2的代码 } finally { // 无论是否出现异常都执行的代码 } ``` 在try块中编写可能引发异常的代码,当异常发生时,程序跳转到对应的catch块中,执行相应的异常处理代码。如果没有合适的catch块来处理异常,程序终止执行并抛出异常。finally块中的代码无论是否出现异常都执行。 具体到C#的实例中,引用给出了使用try-catch处理异常的示例代码。这段代码通过int.Parse()方法将用户输入的字符串转换为整数,并处理可能的格式错误、溢出和空输入等异常情况。 总而言之,C#中的try-catch语句允许我们捕获并处理异常,以确保程序的稳定性和可靠性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#中的try catch finally用法分析](https://download.csdn.net/download/weixin_38697171/12807576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C#语法中try-catch的用法](https://blog.csdn.net/weixin_41883890/article/details/124725452)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C# try catch](https://blog.csdn.net/CAO11021/article/details/107672348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值