C# 语句异常处理语句
2016-04-21 18:49
555 查看
1,try....catch...finally 不会找到逻辑错误,try里面放检测代码,,catch 捕捉到的异常,怎样处理finally不管有没有异常都会执行 try catch finally 3种组合2,finally 很顽强 return后仍然会执行3,
4,
excption的用法
exception是所有异常的父类5,自定义异常错误例子: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 { }
}
}
}
4,
excption的用法
exception是所有异常的父类5,自定义异常错误例子: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#中用来统计你所写的代码总量的代码
- C#中的结构体----面向对象的编程很少用
- C#编程基础 实验(7) (1-2)
- C#数字千分位问题
- 《CLR via C#:框架设计》读书笔记 - 接口
- 《CLR via C#:框架设计》读书笔记 - 事件
- C#中的表达式树的浅解
- 《CLR via C#:框架设计》读书笔记 - 参数
- 《CLR via C#:框架设计》读书笔记 - 方法
- 《CLR via C#:框架设计》读书笔记 - 类型基础
- c#简答题
- C# 比较字符串是否相等 0009
- 《CLR via C#:框架设计》读书笔记 - CLR
- c# 委托学习文档
- 《编写高质量代码 : 改善C#程序的157个建议》读书笔记 11-20
- C# string 对象 0008
- 《C# in Depth:深入理解C#》读书笔记 - LINQ
- 《C# in Depth:深入理解C#》读书笔记 - 扩展方法