New*_*337 0 c# exception custom-exceptions
我试图了解如何以正确的方式使用自定义异常.
我已经多次使用过try/catch但是从来没有说过何时使用自己的类关闭异常.我已经阅读并观看了许多教程,但我无法理解这一点.
这是我的CustomException班级:
[Serializable]
class CustomException : FormatException
{
/// <summary>
/// Just create the exception
/// </summary>
public CustomException()
: base() {
}
/// <summary>
/// Create the exception with description
/// </summary>
/// <param name="message">Exception description</param>
public CustomException(String message)
: base(message) {
}
/// <summary>
/// Create the exception with description and inner cause
/// </summary>
/// <param name="message">Exception description</param>
/// <param name="innerException">Exception inner cause</param>
public CustomException(String message, Exception innerException)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用它的地方:
/// <summary>
/// Checks if parse works
/// </summary>
/// <returns></returns>
public static int ParseInput(string inInt)
{
try
{
int input = int.Parse(inInt);
return input;
}
catch (CustomException)
{
throw new CustomException();
}
catch (Exception ex)
{
MessageBox.Show("Use only numbers! " + ex.Message);
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我做错了什么?请问这个程序崩溃了int input = int.Parse(inInt);,它永远不会出现在我的自定义异常中?如果我使用经典Exception类,那一切都有效.
| 归档时间: |
|
| 查看次数: |
695 次 |
| 最近记录: |