概述
捕获异常的例子: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# 语句异常处理语句所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复