为什么我总是让我异常[可序列化]? (。净) [英] Why should I always make my Exceptions [serializable]? (.NET)

查看:322
本文介绍了为什么我总是让我异常[可序列化]? (。净)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参见什么是正确的方法,使一个自定义的.NET异常序列化?
是否所有的.NET异常序列化? ...

Referring to What is the correct way to make a custom .NET Exception serializable?
and Are all .NET Exceptions serializable? ...

为什么我的例外是可序列化?
有人说:这可以被认为是一个错误,如果由第三方库中定义一个自定义异常,是不是序列化。为什么?

Why should my exceptions be serializable?
Someone said "it can be considered a bug" if a custom exception defined by a third party library, is not serializable. Why?

为什么有例外比其他类在这方面有什么不同?

Why are exceptions different than other classes in this regard?

推荐答案

由于你的异常可能需要不同的应用程序域之间进行整理,如果他们不(正常),序列化,你将失去precious调试信息。不像其他类,你会不会在你的异常是否会被编组控制 - 它会

Because your exceptions may need to be marshalled between different AppDomains and if they aren't (properly) serializable you will lose precious debugging information. Unlike other classes, you won't have control over whether your exception will be marshalled -- it will.


当我的意思是你不会有控制的我的意思是,你通常会产生类有存在的有限空间和存在是众所周知的。如果它是一个返回值,并有人试图把它在不同的AppDomain(或在不同的机器),他们会得到一个错误,并且可以只说不要用这种方式。在调用方知道他们必须将其转换成可以(通过包装方法调用)被序列类型。但是,由于异常被冒泡到顶部,如果没有抓到他们可以超越AppDomain的边界,你甚至不知道你有。您的自定义应用程序异常20级深在不同的AppDomain可能会在main()中的异常报告一路上没有什么会转换成一个序列化的例外你。

When I mean "you won't have control" I mean that classes you create generally have a finite space of existence and the existence is well known. If it's a return value and someone tries to call it in a different AppDomain (or on a different machine) they will get a fault and can just say "Don't use it that way." The caller knows they have to convert it into a type that can be serialized (by wrapping the method call). However since exceptions are bubbled up to the very top if not caught they can transcend AppDomain boundaries you didn't even know you had. Your custom application exception 20 levels deep in a different AppDomain might be the exception reported at Main() and nothing along the way is going to convert it into a serializable exception for you.

这篇关于为什么我总是让我异常[可序列化]? (。净)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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