---
title: "Exception"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/csharp-exception-handling/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/02-编程语言与运行时/.NET与CSharp/Exception.md"
content_hash: 6803cb00e5347a3252bd5cac0546e5acfd37718c9f5e1a480b418d34bf424d63
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# Exception

> 创建时间：2020/6/1 11:34

## throw的异常路径问题

参考:<https://blog.csdn.net/wangtian2014/article/details/51593789>

  1. 例1throw的时候使用了参数ex，虽然会抛出同样的异常，但是异常的堆栈信息缺在throw的时候被改变了，我们在接下来的try块中无法获取异常的原始信息。

  2. 例2没有参数，这个无参的throw可以看做为rethrow（即继续抛出），他会抛出之前捕获到的所有异常。

  3. 例3则完全是一个新的异常，class1中抛出的异常被当做内部异常，存放在inner exception中

```csharp
class MyClass&lt;T, U&gt;
    where T : class
    where U : struct
{ }

```
```csharp
class MyClass&lt;T, U&gt;
    where T : class
    where U : struct
{ }

```
```csharp
class MyClass&lt;T, U&gt;
    where T : class
    where U : struct
{ }

```

## InnerException

当 Y上一个异常的直接结果引发 X 异常时，X 的 InnerException 属性应包含对 Y的引用。
你可以创建一个新的异常来捕获更早的异常。 处理第二个异常的代码可以利用以前异常中的其他信息来更正确地处理错误。

```csharp
class MyClass&lt;T, U&gt;
    where T : class
    where U : struct
{ }

```

新的异常可以添加新的信息，新异常的堆栈信息包含InnerException的堆栈信息
