c#异常处理示例分享
时间:2022-09-10 23:20:54
using System;
using System.Collections.Generic;
using System.Linq; using System.Text;
//2014.3.14
namespace _6.异常
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Convert之前");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Convert之后");
}
catch (Exception ex)
{
Console.WriteLine("输入错误:"+ex.Message+"异常堆栈:"+ex.StackTrace);
}
try
{
Console.WriteLine("请输入你的年龄:");
int s = Convert.ToInt32(Console.ReadLine());
string desc = GetAgeDesc(s);
Console.WriteLine(desc);
}
catch (Exception ex)
{
Console.WriteLine("数据错误,"+ex.Message);
}
Console.ReadKey();
}
static string GetAgeDesc(int age)
{
if (age >= 0 && age <= 3)
{
return "婴幼儿";
}
else if (age > 3 && age < 18)
{
return "青少年";
}
else if (age >=18 && age < 60)
{
return "成年人";
}
else if (age >= 60 && age < 100)
{
return "老年人";
}
else
{
throw new Exception("自己创建的ex.Message");
}
}
}
}
标签:c#,异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Studio中使用jni进行opencv开发的环境配置方法
2021-07-26 00:02:06
![](https://img.aspxhome.com/file/2023/4/102624_0s.jpg)
C#将指定目录所有文件名转换成小写的方法
2023-01-25 23:21:03
java实现捕鱼达人游戏
2023-11-23 21:28:59
![](https://img.aspxhome.com/file/2023/6/70926_0s.jpg)
解决mybatis 中collection嵌套collection引发的bug
2023-03-20 20:55:39
Toast类避免显示时间叠加的方法
2021-08-20 02:01:42
C#使用NPOI上传excel
2022-05-20 14:46:22
![](https://img.aspxhome.com/file/2023/1/94561_0s.jpg)
Android自定义VIew实现卫星菜单效果浅析
2022-09-23 22:44:43
![](https://img.aspxhome.com/file/2023/4/115984_0s.png)
深入解析java中的locale
2023-11-09 18:14:20
解析JavaSe的内部类
2022-08-14 18:19:33
java理论基础Stream管道流状态与并行操作
2021-08-31 14:24:07
![](https://img.aspxhome.com/file/2023/6/87206_0s.jpg)
Android5.0+ CollapsingToolbarLayout使用详解
2022-07-01 01:20:25
![](https://img.aspxhome.com/file/2023/8/98648_0s.jpg)
Spring注解Autowired的底层实现原理详解
2022-10-19 11:49:44
![](https://img.aspxhome.com/file/2023/7/132227_0s.jpg)
Android仿qq顶部消息栏效果
2021-10-28 13:52:57
![](https://img.aspxhome.com/file/2023/1/103901_0s.jpg)
Android中EditText显示明文与密码的两种方式
2021-09-13 07:35:39
![](https://img.aspxhome.com/file/2023/1/137681_0s.gif)
Java实现学生选课管理系统
2023-04-12 20:00:16
![](https://img.aspxhome.com/file/2023/8/88068_0s.jpg)
java实现一个简单的网络爬虫代码示例
2021-08-05 13:59:12
Spring中BeanFactory与FactoryBean接口的区别详解
2022-06-17 11:22:22
![](https://img.aspxhome.com/file/2023/1/66501_0s.jpg)
SpringBoot配置及使用Schedule过程解析
2021-12-31 18:47:32
asp.net实现遍历Request的信息操作示例
2022-11-15 23:15:18
![](https://img.aspxhome.com/file/2023/3/126053_0s.png)
Java实现的不同图片居中剪裁生成同一尺寸缩略图功能示例
2023-08-23 14:53:15