我不确定这是否可能。(我认为应该)。是否可以在不抛出新异常的情况下捕获自定义异常,如下所示?
try
{
//Some logic which throws exception
}
catch (Exception)
{
throw new CustomException();
}
我想要的如下:
try
{
//Some logic which throws exception
}
catch (CustomException)
{
// Handles the exception
}
我已经尝试了上述方法,但它没有直接捕获我的 CustomException。我必须做“ throw new CustomException()
”,这并不理想。我究竟做错了什么?
我的自定义异常类如下所示:
[Serializable]
public class CustomException : Exception
{
public CustomException()
: base() { }
public CustomException(string message)
: base(message) { }
public CustomException(string format, params object[] args)
: base(string.Format(format, args)) { }
public CustomException(string message, Exception innerException)
: base(message, innerException) { }
public CustomException(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }
protected CustomException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
谢谢,