在自定义异常类中实现IDisposable [英] Implement IDisposable in custom exception class

查看:109
本文介绍了在自定义异常类中实现IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不应该在构建自定义异常类时实现IDisposable?

Why we should not implement IDisposable when building a custom exception class?

推荐答案

1。 IDisposable 接口提供了一种释放非托管资源的机制,因此只有在使用它们时才应该使用它:文件流,数据库连接,套接字,内存分配等。查看详细信息在 MSDN [ ^ ]



2.如果是自定义异常,你应该遵守这里描述的规则:

http://msdn.microsoft.com/en-us/library/vstudio/ms229064(v = VS.100)的.aspx [ ^ ]
1. IDisposable interface provides a mechanism for releasing unmanaged resources, so it should be used only if you use them like: file streams, database connections, sockets, memory allocation, etc. See details in MSDN[^]

2.In the case of custom exception you should fallow the rules described here:
http://msdn.microsoft.com/en-us/library/vstudio/ms229064(v=vs.100).aspx[^]


因为你要抛出异常,所以不会有任何人处理它。异常处理程序通常基于基类异常类是通用的,并且不准备处理可处理的资源。
Because you are going to throw the exception and there won't be anyone then to dispose of it. Exception handlers are often generic based on the base exception class and won't be ready to handle a disposable resource.


这篇关于在自定义异常类中实现IDisposable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆