创建您自己的异常并在 C# 中使用它
Creating your own exception and use it in C#
我正在尝试了解如何以正确的方式使用自定义异常。
我已经多次使用 try/catch,但一直不明白何时使用您自己的 class 关闭异常。我已经阅读并观看了很多教程中的一些内容,但我无法理解这个问题。
这是我的 CustomException
class:
[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)
{
}
}
这是我尝试使用它的地方:
/// <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;
}
}
现在我做错了什么?因为程序在这一行崩溃 int input = int.Parse(inInt);
,它从来没有出现过我的自定义异常?如果我使用 classic Exception
class 一切正常。
您定义的 CustomException 比基础 FormatException 更具体 class(这就是继承的含义)。您不能用更具体的异常捕获更通用的异常。只能反过来了。
在您的代码中,您只是在之前捕获异常时抛出您的异常类型,这永远不会发生,因为系统不会抛出它。
通常你会做这样的事情。
try
{
// do something
if (some condition)
throw new MyCustomException() ;
}
catch (SomeOtherException e)
{
// Handle other exceptions
}
在您的情况下,系统会抛出 FormatException
,但 不会 您自己的异常 class,因为它不知道。由于您的异常 class 比 FormatException
更具体,因此不会调用 catch 块。但是,它应该像这样工作:
public static int ParseInput(string inInt)
{
try
{
int input = int.Parse(inInt);
return input;
}
catch (FormatException e)
{
throw new CustomException("Format error!", e);
}
catch (Exception ex)
{
MessageBox.Show("Use only numbers! " + ex.Message);
return -1;
}
}
此外,您的代码有一个缺陷:当任何 Exception
被捕获时,将向用户显示一条消息,-1 将是 return 值(-1 不是有效输入...?)。当 FormatException
被捕获时,错误处理留给被调用者通过重新抛出异常 - 为什么?
请记住,例外情况不会 "fall through":
try
{
}
catch (FormatException e1)
{
// This will throw an exception, but it will **not** be caught here,
// but control goes back to the caller
throw new Exception();
}
catch (Exception e2)
{
// This block will not get called when an exception occurs in
// above handler. It will only get called when an exception other
// than FormatException occurs.
}
嗯,这是因为 int.Parse(int )
在您的案例中引发了 FormatException
并且对您的自定义异常一无所知 (John_Snow_face.jpg)。
您可以使用 TryParse
方法:
int number;
bool result = Int32.TryParse(value, out number);
if(!result)
throw new CustomException("oops");
让我从一个问题开始我的回答:当代码中 没有 出现给定异常时,您认为如何捕获它?
代码在这里:
int input = int.Parse(inInt);
在出现错误时抛出 FormatException
,但不会 您自己的 CustomException
。
您必须将它放入您自己的代码中才能捕获它:
try
{
…
// the execution of this code needs to lead to throwing CustomException…
throw new CustomException();
…
}
// … for a handler of CustomException to make any sense whatsoever
catch (CustomException e)
{
… handle here …
}
我正在尝试了解如何以正确的方式使用自定义异常。
我已经多次使用 try/catch,但一直不明白何时使用您自己的 class 关闭异常。我已经阅读并观看了很多教程中的一些内容,但我无法理解这个问题。
这是我的 CustomException
class:
[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)
{
}
}
这是我尝试使用它的地方:
/// <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;
}
}
现在我做错了什么?因为程序在这一行崩溃 int input = int.Parse(inInt);
,它从来没有出现过我的自定义异常?如果我使用 classic Exception
class 一切正常。
您定义的 CustomException 比基础 FormatException 更具体 class(这就是继承的含义)。您不能用更具体的异常捕获更通用的异常。只能反过来了。
在您的代码中,您只是在之前捕获异常时抛出您的异常类型,这永远不会发生,因为系统不会抛出它。
通常你会做这样的事情。
try
{
// do something
if (some condition)
throw new MyCustomException() ;
}
catch (SomeOtherException e)
{
// Handle other exceptions
}
在您的情况下,系统会抛出 FormatException
,但 不会 您自己的异常 class,因为它不知道。由于您的异常 class 比 FormatException
更具体,因此不会调用 catch 块。但是,它应该像这样工作:
public static int ParseInput(string inInt)
{
try
{
int input = int.Parse(inInt);
return input;
}
catch (FormatException e)
{
throw new CustomException("Format error!", e);
}
catch (Exception ex)
{
MessageBox.Show("Use only numbers! " + ex.Message);
return -1;
}
}
此外,您的代码有一个缺陷:当任何 Exception
被捕获时,将向用户显示一条消息,-1 将是 return 值(-1 不是有效输入...?)。当 FormatException
被捕获时,错误处理留给被调用者通过重新抛出异常 - 为什么?
请记住,例外情况不会 "fall through":
try
{
}
catch (FormatException e1)
{
// This will throw an exception, but it will **not** be caught here,
// but control goes back to the caller
throw new Exception();
}
catch (Exception e2)
{
// This block will not get called when an exception occurs in
// above handler. It will only get called when an exception other
// than FormatException occurs.
}
嗯,这是因为 int.Parse(int )
在您的案例中引发了 FormatException
并且对您的自定义异常一无所知 (John_Snow_face.jpg)。
您可以使用 TryParse
方法:
int number;
bool result = Int32.TryParse(value, out number);
if(!result)
throw new CustomException("oops");
让我从一个问题开始我的回答:当代码中 没有 出现给定异常时,您认为如何捕获它?
代码在这里:
int input = int.Parse(inInt);
在出现错误时抛出 FormatException
,但不会 您自己的 CustomException
。
您必须将它放入您自己的代码中才能捕获它:
try
{
…
// the execution of this code needs to lead to throwing CustomException…
throw new CustomException();
…
}
// … for a handler of CustomException to make any sense whatsoever
catch (CustomException e)
{
… handle here …
}