如何在 C# 中使用基于自定义异常构造的函数抛出自定义异常?
How to throw custom exceptions using functions based on custom exception construct in C#?
我有一个自定义 Exception
构造,我想使用函数而不是使用 throw new ExceptionName("msg")
来抛出自定义异常。
我阅读了 Best practices for exceptions 并使用 VB
而非 C#
成功实现了它。
这是我的自定义构造:
[Serializable()]
public class ArtCoreExceptions : Exception
{
public ArtCoreExceptions()
{
}
public ArtCoreExceptions(string message)
: base(message)
{
}
public ArtCoreExceptions(string message, Exception inner)
: base(message, inner)
{
}
protected ArtCoreExceptions(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
{ }
}
我尝试定义一个函数,如:
UploadFailureException ArtCoreExceptions()
{
string description = "My NewFileIOException Description";
return new ArtCoreExceptions(description);
}
但是编译器给了我 "UploadFailureException "
的缺失引用错误
背景
在 VB
我曾经做过以下事情:
Public Class ArtCoreExceptions
Inherits Exception
Public Sub New()
End Sub
Public Sub New(message As String)
MyBase.New(message)
End Sub
Public Sub New(message As String, inner As Exception)
MyBase.New(message, inner)
End Sub
Public Sub New(ByVal info As SerializationInfo, context _
As StreamingContext)
MyBase.New(info, context)
End Sub
End Class
Public Function UploadFailureException() As ArtCoreExceptions
Dim description As String = "File could not be uploaded"
Return New ARTSQLExceptions(description)
End Function
而且我曾经 throw UploadFailureException
作为例外,我怎样才能在 C#
中做同样的事情
你把这里搞砸了。 UploadFailureException
是方法的名称(或您喜欢调用的任何函数),而 ArtCoreExceptions
是 class 名称。
ArtCoreExceptions UploadFailureException ()
{
string description = "My NewFileIOException Description";
return new ArtCoreExceptions(description);
}
我假设你想要 return ArtCoreExceptions
。方法签名中的 return 值出现在 之前 它的名称,而不是 之后 它。
ReturnType MethodName(params) { ... }
与 VB 相比,您在 之前写函数的名称 它是 return-type:
Function MethodName() As ReturnType
所以请改用 ArtCoreExceptions UploadFailureException ()
{ ... }。
Public Function UploadFailureException() As ArtCoreExceptions
Dim description As String = "File could not be uploaded"
Return New ARTSQLExceptions(description)
End Function
上面的代码创建了一个函数 UploadFailureException() ,其 return 类型是 ArtCoreExceptions 类型的对象。您可以在 C# 中执行以下操作
public ArtCoreExceptions UploadFailureException()
{
string description = "My NewFileIOException Description";
return new ArtCoreExceptions(description);
}
其中 return 是 class ArtCoreExceptions
派生自 System.Exception.
的实例
我有一个自定义 Exception
构造,我想使用函数而不是使用 throw new ExceptionName("msg")
来抛出自定义异常。
我阅读了 Best practices for exceptions 并使用 VB
而非 C#
成功实现了它。
这是我的自定义构造:
[Serializable()]
public class ArtCoreExceptions : Exception
{
public ArtCoreExceptions()
{
}
public ArtCoreExceptions(string message)
: base(message)
{
}
public ArtCoreExceptions(string message, Exception inner)
: base(message, inner)
{
}
protected ArtCoreExceptions(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
{ }
}
我尝试定义一个函数,如:
UploadFailureException ArtCoreExceptions()
{
string description = "My NewFileIOException Description";
return new ArtCoreExceptions(description);
}
但是编译器给了我 "UploadFailureException "
的缺失引用错误背景
在 VB
我曾经做过以下事情:
Public Class ArtCoreExceptions
Inherits Exception
Public Sub New()
End Sub
Public Sub New(message As String)
MyBase.New(message)
End Sub
Public Sub New(message As String, inner As Exception)
MyBase.New(message, inner)
End Sub
Public Sub New(ByVal info As SerializationInfo, context _
As StreamingContext)
MyBase.New(info, context)
End Sub
End Class
Public Function UploadFailureException() As ArtCoreExceptions
Dim description As String = "File could not be uploaded"
Return New ARTSQLExceptions(description)
End Function
而且我曾经 throw UploadFailureException
作为例外,我怎样才能在 C#
你把这里搞砸了。 UploadFailureException
是方法的名称(或您喜欢调用的任何函数),而 ArtCoreExceptions
是 class 名称。
ArtCoreExceptions UploadFailureException ()
{
string description = "My NewFileIOException Description";
return new ArtCoreExceptions(description);
}
我假设你想要 return ArtCoreExceptions
。方法签名中的 return 值出现在 之前 它的名称,而不是 之后 它。
ReturnType MethodName(params) { ... }
与 VB 相比,您在 之前写函数的名称 它是 return-type:
Function MethodName() As ReturnType
所以请改用 ArtCoreExceptions UploadFailureException ()
{ ... }。
Public Function UploadFailureException() As ArtCoreExceptions
Dim description As String = "File could not be uploaded"
Return New ARTSQLExceptions(description)
End Function
上面的代码创建了一个函数 UploadFailureException() ,其 return 类型是 ArtCoreExceptions 类型的对象。您可以在 C# 中执行以下操作
public ArtCoreExceptions UploadFailureException()
{
string description = "My NewFileIOException Description";
return new ArtCoreExceptions(description);
}
其中 return 是 class ArtCoreExceptions
派生自 System.Exception.