异常处理写法
- 一个
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);
}
}