我试图在C#中序列化Exception对象.但是,似乎不可能,因为Exception类没有标记为[Serializable].有办法解决这个问题吗?
如果在执行应用程序期间出现问题,我希望被告知发生的异常.
我的第一反应是序列化它.
在C#中实现自定义异常的行业标准最佳实践是什么?
我检查了谷歌,并提出了大量建议,但我不知道哪些建议具有更高的可信度.
如果任何人有权威文章的链接,那也会有所帮助.
假设一个简单的示例,其中方法检索集合(例如包含一些配置字符串的列表)并尝试以某种方式检查它:
void Init()
{
XmlDocument config = new XmlDocument();
config.Load(someXml);
var list = config.SelectNodes("/root/strings/key"); // Normally, list should not be null or empty
if (list == null || list.Count == 0)
throw new SomeExceptionType(message); // What kind of exception to throw?
// Iterate list and process/examine its elements
foreach (var e in list) ...
}
Run Code Online (Sandbox Code Playgroud)
在此特定实例中,如果未检索到任何内容,则该方法无法正常继续.我不确定在这种情况下抛出什么异常类型.据我所知,我的选择是:
手动抛出任何东西,让它NullReferenceException自动抛出(不处理空列表情况),
抛出自定义异常类型(可能不是一个好主意,因为我不期望调用者会尝试对异常做任何事情,即他不会寻找一个特殊的异常类型来处理),
我有一个自定义异常类,如下所示:
<Serializable>
Public Class SamException
Inherits Exception
Public Sub New()
' Add other code for custom properties here.
End Sub
Public Property OfferBugSend As Boolean = True
Public Sub New(ByVal message As String)
MyBase.New(message)
' Add other code for custom properties here.
End Sub
Public Sub New(ByVal message As String, ByVal inner As Exception)
MyBase.New(message, inner)
' Add other code for custom properties here.
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
我在AJAX响应的返回中使用它来处理某些情况.
在我的AJAX响应的错误函数中,我确定错误属于我的自定义类型,如下所示:
....
ajax code....
.error = function (xhr, text, message) { …Run Code Online (Sandbox Code Playgroud) vb.net exception-handling exception custom-errors custom-error-handling
经过一些研究后,我发现自定义异常应如下所示:
using System;
using System.Runtime.Serialization;
namespace YourNamespaceHere
{
[Serializable()]
public class YourCustomException : Exception, ISerializable
{
public YourCustomException() : base() { }
public YourCustomException(string message) : base(message) { }
public YourCustomException(string message, System.Exception inner) : base(message, inner) { }
public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
Run Code Online (Sandbox Code Playgroud)
但我有小问题.
我希望上面的例外有两个额外的字段,比如说int ID和int ErrorCode.如何添加这两个字段并初始化它们 - 我应该添加一个新的构造函数,这两个参数和消息参数?
你也可以帮助我并展示如何为这个具有两个新属性的新类编写序列化方法吗?
谢谢.
我有以下用c#编写的代码.
自从我将NuGet SonarAnalyzer.CSharp软件包升级到版本6.0.0.2033后,我遇到了这个错误:
错误S3925更新"ISerializable"的此实现以符合推荐的序列化模式.
public class GenericException : Exception
{
public GenericException(string message) : base(message)
{
// ...
}
public GenericException(string message, Exception innerException) : base(message, innerException)
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?
我必须使用一个旧的应用程序,它使用binaryFormatter将应用程序数据序列化到文件流中(例如在一个名为"data.oldformat"的文件中),而没有任何优化主类已经标记了属性
<serializable()>public MainClass
.......
end class
Run Code Online (Sandbox Code Playgroud)
和序列化代码
dim b as new binaryformatter
b.serialize(mystream,mymainclass)
Run Code Online (Sandbox Code Playgroud)
为了优化序列化/反序列化过程,我简单地使类实现了ISerializable接口并编写了一些优化的序列化例程
<serializable()>public MainClass
implements ISerializable
.......
end class
Run Code Online (Sandbox Code Playgroud)
优化工作非常好但我必须找到一种方法来恢复旧文件中的数据以实现向后兼容.
我怎样才能做到这一点??
皮耶路易吉
我正在尝试微调我的MVC应用程序中的错误处理.
我在web.config中启用了自定义错误,并添加了以下代码Application_Error.
Global.asax中
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError() as Exception;
if (exception != null)
{
Context.ClearError();
Context.Response.TrySkipIisCustomErrors = true;
string path = (exception is HttpException && (exception as HttpException).GetHttpCode() == 404) ?
"~/Error/NotFound" :
"~/Error/Index";
Context.Server.TransferRequest(path, false);
}
}
Run Code Online (Sandbox Code Playgroud)
ErrorController.cs
[AllowAnonymous]
public ActionResult Index()
{
Response.Clear();
Response.StatusCode = 503;
Response.TrySkipIisCustomErrors = true;
return View();
}
[AllowAnonymous]
public ActionResult NotFound()
{
Response.Clear();
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View();
} …Run Code Online (Sandbox Code Playgroud)