如何处理用户定义的异常,并在处理完之后恢复程序流程 [英] How can I handle user defined exceptions and after handling them resume the flow of program

查看:91
本文介绍了如何处理用户定义的异常,并在处理完之后恢复程序流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/**
 * 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);
    }
}

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

How do I use this in a program and then resume for there? I do not want the program to end but want to handle the exception and continue.

推荐答案

您需要使用尝试 / 捕获。您可以从Sun(Oracle)的异常中了解很多有关异常处理的信息。处理教程。在该教程中,请参见有关捕获和处理该特定地址的部分,以解决您的问题。

You need to use try/catch. You can learn a lot about exception handling from Sun's (Oracle's) exception handling tutorials. In that tutorial look at the sections about Catching and Handling that specific address your question.

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

For example, in the code that calls the method that may throw this exception:

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...

这篇关于如何处理用户定义的异常,并在处理完之后恢复程序流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆