英文:
How do I send an error to the constructor of the Exception class in addition to string?
问题
我试图将异常发送到基本构造函数,但我不知道如何发送它。
我需要创建一个异常吗?
这是类的代码:
public class InvalidEntityException : Exception
{
public InvalidEntityException(string message, Exception ex) : base(message, ex) { }
}
异常看起来像这样:
> throw new InvalidEntityException( "Add function :: DAL", ???????);
我应该如何抛出异常?
英文:
I am trying send exceptions to the base constructor but I don't know how to send it.
Should I need create an exception?
Here the code of the class:
public class InvalidEntityException : Exception
{
public InvalidEntityException(string message, Exception ex) : base(message, ex) { }
}
The exception look like this:
> throw new InvalidEntityException( "Add function :: DAL", ???????);
How do I need throw the exceptions?
答案1
得分: 1
以下是翻译好的内容:
如果你在一个catch
子句内或者有一个当前的异常对象可用,那么请使用该异常对象。
try
{
// 代码
}
catch (Exception ex)
{
throw new InvalidEntityException("Add function :: DAL", ex);
}
如果你没有当前的异常对象,只需传递null
。
throw new InvalidEntityException("Add function :: DAL", null);
来自Exception
类构造函数的文档:
public Exception (string? message, Exception? innerException);
innerException
异常作为当前异常的原因的异常,或者如果未指定内部异常,则为null引用(在Visual Basic中为Nothing)。
英文:
If you are inside a catch
clause or have a current exception object available otherwise, use that exception object.
try
{
// code
}
catch(Exception ex)
{
throw new InvalidEntityException("Add function :: DAL", ex);
}
If you don't have a current exception object, just pass null
.
throw new InvalidEntityException("Add function :: DAL", null);
From the documentation of the Exception
class constructor:
> public Exception (string? message, Exception? innerException);
>
> innerException
Exception
>
> The exception that is the cause of the current exception, or a null
> reference (Nothing in Visual Basic) if no inner exception is
> specified.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论