0
点赞
收藏
分享

微信扫一扫

C#(二十八):异常处理


异常处理写法

  • 一个​​try​​​可以匹配多个​​catch​
  • 多个​​catch​​之间的顺序必须是父类在最后

try
{
string[] strArr = new string[3];
strArr[4] = "Lee";
}
catch (Exception e)
{
Console.WriteLine(e);
}

自定义异常

class Program
{
static void Main(string[] args)
{
try {
ScoreExceptionDemo();
}
catch (Exception e)
{
Console.WriteLine(e); // 自定义异常描述
}
}

static void ScoreExceptionDemo() {
ScoreException scoreException = new ScoreException("自定义异常描述");
throw scoreException;
}
}

// 自定义异常
class ScoreException : Exception {
public ScoreException(string message) : base(message) {
Console.WriteLine(message);
}
}


举报

相关推荐

0 条评论