C# 语句异常处理语句
来源:互联网 发布:mac 待机快捷键 编辑:程序博客网 时间:2024/06/13 21:22
捕获异常的例子:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 自定义异常
{
class Triangle
{
public int a;
public int b;
public int c;
public int getCircumference()
{
if (a + b <=c || a + c <=b || b + c <=a) throw new Myexception();
return a + b + c;
}
public void showInfo()
{
if (a + b <= c || a + c <= b || b + c <= a) throw new Myexception();
Console.WriteLine("三边长为{0},{1},{2}", a, b, c);
}
}
class Myexception:Exception
{
public Myexception(): base("InvalidTriangleException")
{
}
//public Myexception(string message): base(message)
//{
//}
//public Myexception (string message, Exception inner) : base(message, inner)
//{
//}
}
class Program
{
static void Main(string[] args)
{
Triangle t=new Triangle ();
t.a = int.Parse(Console.ReadLine());
t.b = int.Parse(Console.ReadLine());
t.c = int.Parse(Console.ReadLine());
try
{
Console.WriteLine(t.getCircumference());
t.showInfo();
}
catch(Myexception me)
{
Console.WriteLine(me.Message + "异常!");
}
finally
{
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication44
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[4];
try
{
for (int i = 0; i < 5; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
}
catch (Exception e1) ///此处Exception 可以具体到子类
{
Console.WriteLine( e1.Message+"异常!");
}
finally { }
}
}
}
- C# 语句异常处理语句
- C#的异常处理语句
- C#的throw异常处理语句
- C#中异常处理语句Finally
- C#2继承、foreach语句及异常处理语句
- 异常处理语句
- C#中的异常语句
- 我的c#之路(8.异常处理语句)
- 黑马程序员----C#中的流程控制语句之异常处理语句
- 动态sql语句 异常处理
- try语句和异常处理
- C++ Primer 学习笔记10 语句 (空语句、if语句、switch语句、for语句、dowhile语句、break语句、goto语句、continue语句、try块和异常处理)
- 异常处理的try-catch-finally语句
- JAVA-使用异常处理语句的注意事项
- 异常处理相关 try/finally语句
- sql语句处理单引号,解决查询异常
- sql/plus循环语句和异常处理
- java 异常处理的语句执行
- . netframework
- C#中用来统计你所写的代码总量的代码
- C# 控制台下的常见函数&数据类型
- 值类型和引用类型
- C#语句 运算符 表达式 字符串 编码
- C# 语句异常处理语句
- C#运算符 string
- C# 数组中的数组
- SharePoint轻量化应用之HR系统构建-人才归档 及隐藏参数界面设计
- C#集合
- 类 对象
- C#中 类的成员
- 索引器
- 委托事件