C# C中使用属性类处理自定义异常#
我知道这个问题已经被问到了。我需要开发用户定义的异常处理以及属性类,但我不会检索新添加的属性(DSMException.cs properties)异常。我经历了这两种解决方案- 我有一个C# C中使用属性类处理自定义异常#,c#,.net,C#,.net,我知道这个问题已经被问到了。我需要开发用户定义的异常处理以及属性类,但我不会检索新添加的属性(DSMException.cs properties)异常。我经历了这两种解决方案- 我有一个Property类-DSMException,它包含三个属性Type、Message、InnerExceptionMessage,需要对其进行dsiplayed DSMException.cs public class DSMException { public string Ty
Property
类-DSMException,它包含三个属性Type、Message、InnerExceptionMessage,需要对其进行dsiplayed
DSMException.cs
public class DSMException
{
public string Type { get; set; }
public string Message { get; set; }
public string InnerExceptionMessage { get; set; }
}
public class FatalException :Exception
{
DSMException exception = new DSMException();
public FatalException():base()
{
}
public FatalException(string? Message): base(Message)
{
exception.Type = "FATAL";
exception.Message = "MESSAGE:" + Message;
}
public FatalException(string? Message,Exception ex) : base(Message,ex)
{
exception.Type = "FATAL";
exception.Message = "MESSAGE: " + ex.Message;
exception.InnerExceptionMessage = "MORE DET: " + ex.ToInnerMostException().Message;
}
}
public override void Run(ServiceSettings settings, DataSource dataSource, string path)
{
try
{
var channel = entities.Channels
.Where(c => c.Name == parameters.Channel)
.FirstOrDefault();
if (channel != null)
{
// ANY LOGIC
}
else
{
throw new FatalException("Invalid Channel Name !!!");
}
}
catch (FatalException ex)// for channel is null
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
给出了相应的自定义异常类
FatalException.cs
public class DSMException
{
public string Type { get; set; }
public string Message { get; set; }
public string InnerExceptionMessage { get; set; }
}
public class FatalException :Exception
{
DSMException exception = new DSMException();
public FatalException():base()
{
}
public FatalException(string? Message): base(Message)
{
exception.Type = "FATAL";
exception.Message = "MESSAGE:" + Message;
}
public FatalException(string? Message,Exception ex) : base(Message,ex)
{
exception.Type = "FATAL";
exception.Message = "MESSAGE: " + ex.Message;
exception.InnerExceptionMessage = "MORE DET: " + ex.ToInnerMostException().Message;
}
}
public override void Run(ServiceSettings settings, DataSource dataSource, string path)
{
try
{
var channel = entities.Channels
.Where(c => c.Name == parameters.Channel)
.FirstOrDefault();
if (channel != null)
{
// ANY LOGIC
}
else
{
throw new FatalException("Invalid Channel Name !!!");
}
}
catch (FatalException ex)// for channel is null
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
我抛出的异常如下代码所示
Channel.cs
public class DSMException
{
public string Type { get; set; }
public string Message { get; set; }
public string InnerExceptionMessage { get; set; }
}
public class FatalException :Exception
{
DSMException exception = new DSMException();
public FatalException():base()
{
}
public FatalException(string? Message): base(Message)
{
exception.Type = "FATAL";
exception.Message = "MESSAGE:" + Message;
}
public FatalException(string? Message,Exception ex) : base(Message,ex)
{
exception.Type = "FATAL";
exception.Message = "MESSAGE: " + ex.Message;
exception.InnerExceptionMessage = "MORE DET: " + ex.ToInnerMostException().Message;
}
}
public override void Run(ServiceSettings settings, DataSource dataSource, string path)
{
try
{
var channel = entities.Channels
.Where(c => c.Name == parameters.Channel)
.FirstOrDefault();
if (channel != null)
{
// ANY LOGIC
}
else
{
throw new FatalException("Invalid Channel Name !!!");
}
}
catch (FatalException ex)// for channel is null
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
在捕获异常FatalExceptions期间,此处会出现错误
FolderInvocation.cs(错误)
我缺少的是,只要这些最小复制代码是正确的,但我需要在FatalException中获得Ex.Type。请告诉我。如果我理解正确,您只需将字段作为公共属性转发即可:
public FatalException(string? Message): base(Message)
{
Type = "FATAL";
Message = "MESSAGE:" + Message;
}
public string MyType { get ; }
public string MyMessage { get ; }
public string MyInnerExceptionMessage { get ; }
公共类FatalException:异常
{
私有只读DSMCeption _exception=新DSMCeption();
/*…剪断*/
公共字符串MyType=>\u exception.Type;
公共字符串MyMessage=>\u exception.Message;
公共字符串MyInnerExceptionMessage=>\u exception.InnerExceptionMessage;
}
然而,它似乎有点违背了DSMException
类的目的。您可以将DSMException
设置为公共属性,但我强烈建议事先将其设置为不可变
显然,我不知道DSMException
类的预期用途,但是由于所有值都是在构造函数中组装的,因此您也可以直接分配给公共属性:
public FatalException(string? Message): base(Message)
{
Type = "FATAL";
Message = "MESSAGE:" + Message;
}
public string MyType { get ; }
public string MyMessage { get ; }
public string MyInnerExceptionMessage { get ; }
继续不变性建议:您可以这样定义
DSMException
类:
公共密封类异常
{
公共DSM异常(字符串类型、字符串消息、字符串消息)
{
类型=类型;
消息=消息;
InnerExceptionMessage=innerMessage;
}
公共字符串类型{get;}
公共字符串消息{get;}
公共字符串InnerExceptionMessage{get;}
}
。。。然后把它变成公共财产:
公共类FatalException:异常
{
/*…剪断*/
公共DSM异常DSM=>\u异常;
}
看起来您仍然需要在FatalException
、ErrorException
、WarningException
等范围内复制大部分构造函数代码
还要注意的是,只要您的自定义异常只在同一AppDomain中出现,一切都很好;当它们可能跨越域边界时(例如通过有线),
DSMException
需要可序列化,并且还需要实现自定义异常的序列化方法。这里出现了什么问题?您可以添加错误文本吗?您的DSM异常
是私有的。无法在FatalException
实例之外访问它,它是字段,而不是属性。还有四种其他类型的用户定义异常-即FatalException、InfoException、WarningException、ErrorException。我创建了DSMExceptions.cs以减少代码处理这四种类型的异常。更新了我的答案。您介意编辑我的代码吗?由于在初始化FatalExceptions.cs的部分过程中出现了一些错误,因此如果代码按预期工作,则问题不大。:)你能补充一些关于你观察到的错误的细节吗?