Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理用户定义的异常,并在处理后恢复程序流程_Java_Exception - Fatal编程技术网

Java 如何处理用户定义的异常,并在处理后恢复程序流程

Java 如何处理用户定义的异常,并在处理后恢复程序流程,java,exception,Java,Exception,我如何在程序中使用它,然后在那里继续?我不希望程序结束,但希望处理异常并继续。捕获它即可 /** * An exception thrown when an illegal side pit was * specified (i.e. not in the range 1-6) for a move */ public class IllegalSidePitNumException extends RuntimeException { /** * Exception

我如何在程序中使用它,然后在那里继续?我不希望程序结束,但希望处理异常并继续。

捕获它即可

/**
 * An exception thrown when an illegal side pit was
 * specified (i.e. not in the range 1-6) for a move
 */
public class IllegalSidePitNumException extends RuntimeException
{
    /**
     *  Exception constructor.
     *  @param sidePitNum the illegal side pit that was selected.
     */
    public IllegalSidePitNumException(int sidePitNum)
    {
        super("No such side pit number: "+sidePitNum);
    }
}
抓住它

/**
 * An exception thrown when an illegal side pit was
 * specified (i.e. not in the range 1-6) for a move
 */
public class IllegalSidePitNumException extends RuntimeException
{
    /**
     *  Exception constructor.
     *  @param sidePitNum the illegal side pit that was selected.
     */
    public IllegalSidePitNumException(int sidePitNum)
    {
        super("No such side pit number: "+sidePitNum);
    }
}
您需要使用try/catch。您可以从Sun的Oracle中了解很多关于异常处理的知识。在该教程中,请查看有关捕获和处理特定问题的章节

例如,在调用可能引发此异常的方法的代码中:

try {
    doStuffWhichPossiblyThrowsThisException();
} catch (IllegalSidePitNumException e) {
    // Don't rethrow it. I would however log it.
}
continueWithOtherStuff();
您需要使用try/catch。您可以从Sun的Oracle中了解很多关于异常处理的知识。在该教程中,请查看有关捕获和处理特定问题的章节

例如,在调用可能引发此异常的方法的代码中:

try {
    doStuffWhichPossiblyThrowsThisException();
} catch (IllegalSidePitNumException e) {
    // Don't rethrow it. I would however log it.
}
continueWithOtherStuff();

旁注:如果这是一个预期的异常,您可能希望从exception而不是RuntimeException继承它。RuntimeException用于发生意外情况时,例如由于程序员错误而导致的非法输入


当然,您没有给出打算使用异常的上下文,所以这只是理论,但您确实提到要继续执行。

侧注:如果这是预期的异常,您可能希望从异常继承它,而不是从RuntimeException继承。RuntimeException用于发生意外情况时,例如由于程序员错误而导致的非法输入


当然,您没有给出打算使用异常的上下文,因此这只是理论,但您确实提到要继续执行。

正如其他人所说,您可以执行以下操作:

try {
    ...call method here...
} catch (IllegalSidePitNumException e) {
    // Display message (per your comment to BalusC)
    System.err.println(e.getMessage());
    e.printStackTrace(System.err);

    // You can also handle the exception other ways (but do not ignore it)
    // Such as correcting some offending values, setting up for a retry
    // logging the information, throwing a different exception
}

...program continues executing here...
但在Java中无法执行以下操作:

try {
    doSomething();
} catch (SomeException ex) {
    doRecovery();
} 
doSomethingElse();

上面是一个假设的恢复模型异常处理示例,Java不支持这一点。也没有C,C++或任何其他当前流行的编程语言…虽然有一些历史语言支持它。事实上,Java编译器会给您一个以上的编译错误,即抛出后的语句是不可访问的

正如其他人所说,您可以执行以下操作:

try {
    ...call method here...
} catch (IllegalSidePitNumException e) {
    // Display message (per your comment to BalusC)
    System.err.println(e.getMessage());
    e.printStackTrace(System.err);

    // You can also handle the exception other ways (but do not ignore it)
    // Such as correcting some offending values, setting up for a retry
    // logging the information, throwing a different exception
}

...program continues executing here...
但在Java中无法执行以下操作:

try {
    doSomething();
} catch (SomeException ex) {
    doRecovery();
} 
doSomethingElse();

上面是一个假设的恢复模型异常处理示例,Java不支持这一点。也没有C,C++或任何其他当前流行的编程语言…虽然有一些历史语言支持它。事实上,Java编译器会给您一个以上的编译错误,即抛出后的语句是不可访问的

当抛出此类型的专家时,抛出IllegalSidePitNumException。我必须捕捉它,然后显示meesage,然后继续添加一行显示消息,并等待用户单击OK左右。如何做到这一点完全取决于您使用的UI技术。我打赌是秋千。如果为true,则将JOptionPane.showMessageDialognull放入一些消息;在catch block.when中抛出此类型的专家时,抛出IllegalSidePitNumException。我必须捕捉它,然后显示meesage,然后继续添加一行显示消息,并等待用户单击OK左右。如何做到这一点完全取决于您使用的UI技术。我打赌是秋千。如果为true,则将JOptionPane.showMessageDialognull放入一些消息;在挡块中。