/
MatrixTypeCalculateError.cs
94 lines (86 loc) · 2.86 KB
/
MatrixTypeCalculateError.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using static System.Console;
namespace 矩阵类库
{
/// <summary>
/// 进行运算的矩阵类型发生错误
/// </summary>
public class MatrixTypeCalculateError : Exception
{
string ReasonMessage;
string DeepReason;
Operator? op = null;
public MatrixTypeCalculateError() : base()
{
ReasonMessage = "进行运算的矩阵类型发生错误";
}
/// <summary>
/// 构造矩阵运算的新异常
/// </summary>
/// <param name="o">异常运算符 (1 : '+', 2 : '-', 3 : '*', 5 : '乘方')</param>
public MatrixTypeCalculateError(int o) : base()
{
ReasonMessage = "进行运算的矩阵类型发生错误";
try
{
op = (Operator)o;
}
catch (NotImplementedException e)
{
WriteLine(e.Message);
}
switch (o)
{
case 1:
DeepReason = "加法运算时矩阵的类型不相同";
break;
case 2:
DeepReason = "减法运算时矩阵的类型不相同";
break;
case 3:
DeepReason = "乘法运算时矩阵的行列关系不正确(应该为:左操作数的行数=右操作数的列数)";
break;
case 5:
DeepReason = "乘方运算时参数不是方阵";
break;
default:
DeepReason = "向异常类传递的参数应该o∈{1,2,3,4}(未知的异常)";
break;
}
}
/// <summary>
/// 在控制台上打印异常信息
/// </summary>
public void ShowException()
{
try
{
switch (op)
{
case (Operator)1:
WriteLine(ReasonMessage + "," + DeepReason);
break;
case (Operator)2:
WriteLine(ReasonMessage + "," + DeepReason);
break;
case (Operator)3:
WriteLine(ReasonMessage + "," + DeepReason);
break;
case (Operator)4:
WriteLine(ReasonMessage + "," + DeepReason);
break;
case (Operator)5:
WriteLine(ReasonMessage + "," + DeepReason);
break;
default:
WriteLine("无效的操作数信息");
break;
}
}
catch (ArgumentNullException e)
{
WriteLine(e.Message);
}
}
}
}