CS0155 捕获或抛出的值的类型必须从 System.Exception 派生
时间: 2024-10-10 11:02:34
浏览: 12
CS0155 错误通常出现在C#或.NET编程中,当你在 try-catch 或 throw 语句中试图捕获或抛出一个值,但是该值的类型不是从 `System.Exception` 或其派生类来的。`System.Exception` 是所有内置错误和异常类的基础,包括如 `ArgumentNullException`、`InvalidOperationException` 等。
如果你编写了如下代码:
```csharp
try {
// 一些可能导致异常的操作
} catch (MyCustomType exceptionValue) { // MyCustomType 不是从 Exception 派生的
// ...
```csharp
throw new MyCustomType(); // MyCustomType 不是从 Exception 派生的
编译器会报出 CS0155 错误,因为 `MyCustomType` 不符合规范,应该是一个异常类型或者派生自 `Exception`。
修复这个问题的方法很简单,要么将 `MyCustomType` 改为从 `Exception` 或其派生类创建,要么更改捕获或抛出的地方,使用正确的异常类型。
阅读全文
相关推荐











