[英]Custom Exception handing with Property class in C#
我知道这已经被问到了。我需要开发用户定义的异常处理以及属性 class,但我没有检索新添加的属性(DSMException.cs 属性)异常。 我经历了这两个解决方案-
我有一个Property
class -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; }
}
给出了相应的自定义异常 class。
致命异常.cs
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;
}
}
我正在抛出如下代码Channel.cs 之类的异常
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;
}
}
在捕获 Exceptions-FatalExceptions 期间此处出现错误
FolderInvocation.cs (错误)
private void DoFolderInvocation(DataSource dataSource, Plugin plugin)
{
// Do type invocation
try
{
result = helper.Invoke(importableFile.Path);// This invokes the Run method, I skipped those codes
if (result == null)
logger.WriteError($"{dataSource.Key}: Unknown error while processing file '{importableFile.Path}'.");
else if (result.Success == false)
logger.WriteError($"{dataSource.Key}: Error while processing file '{importableFile.Path}'.
{result.Message}");
else
logger.WriteDebug($"{dataSource.Key}: File '{importableFile.Path}' was processed successfully.
{result.Message}");
}
catch (FatalException exFatal)// **Problem Arises here-> need to get Ex.Type in the FatalException**
{
logger.WriteError($"{dataSource.Key}: An error occurred while processing data source:
{exFatal.Message}");
throw exFatal;
}
catch (Exception ex)
{
logger.WriteError($"{dataSource.Key}: Invocation error while processing file '{importableFile.Path}'.
{ex.Message}");
}
}
我所缺少的,就这些最小复制代码而言是正确的,但我需要在 FatalException 中获取 Ex.Type。 请给我建议。
如果我理解正确,您只需将字段作为公共属性转发:
public class FatalException: Exception
{
private readonly DSMException _exception = new DSMException();
/* ... snip ... */
public string MyType => _exception.Type;
public string MyMessage => _exception.Message;
public string MyInnerExceptionMessage => _exception.InnerExceptionMessage;
}
然而,它似乎确实有点违背了DSMException
class 的目的。 您可以将DSMException
公共属性,但我强烈建议您事先将其设为不可变。
我显然不知道DSMException
class 的预期用途,但是由于无论如何所有值都在构造函数中组装,因此您也可以直接分配给公共属性:
public FatalException(string? Message): base(Message)
{
Type = "FATAL";
Message = "MESSAGE:" + Message;
}
public string MyType { get ; }
public string MyMessage { get ; }
public string MyInnerExceptionMessage { get ; }
要继续不变性建议:您可以像这样定义DSMException
class:
public sealed class DSMException
{
public DSMException(string type, string message, string innerMessage)
{
Type = type;
Message = message;
InnerExceptionMessage = innerMessage;
}
public string Type { get; }
public string Message { get; }
public string InnerExceptionMessage { get; }
}
...然后将其设为公共财产:
public class FatalException: Exception
{
/* ... snip ... */
public DSMException DSM => _exception;
}
看来您仍然需要跨FatalException
、 ErrorException
、 WarningException
等复制大部分构造函数代码。
另请注意,只要您的自定义异常只在同一个 AppDomain 中飞来飞去,一切都很好; 当它们可能跨越域边界(例如通过有线)时, DSMException
需要可序列化,并且您的自定义异常的序列化方法也需要实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.