所以我有一个程序,我想检查游戏板中是否有一组线,如果不是,则抛出错误,但继续执行该程序。这是使用的代码(我不担心类/包名称):
package routines;
import java.util.Random;
import game.GameBoard;
import game.Player;
public class MovePlayer extends Routine {
final protected int destX;
final protected int destY;
public MovePlayer(int destX, int destY, GameBoard board) {
super();
if(destY > board.getHeight() || destX > board.getWidth()) {
fail();
throw new RuntimeException(">>> Error while creating routine, one or more coords are outside of the game board");
} else {
this.destX = destX;
this.destY = destY;
}
}
这里是超类的链接(很多代码,不知道我是否应该将所有内容放在这里)Super class SRC
编辑:不确定这是否能解决我想要的问题,但我所做的就是从变量中删除final
关键字
答案 0 :(得分:1)
使用
class Article < Comfy::Cms::Page
include PgSearch
include ActionView::Helpers::SanitizeHelper
HOSTNAME = ENV['HOSTNAME'] || Socket.gethostname
has_many :view_mappings, dependent: :destroy
has_many :favorite_mappings, dependent: :destroy
pg_search_scope :search_by_keywords, against: [:content_cache, :label], using: { tsearch: { any_word: true, prefix: true } }
pg_search_scope :search_by_category, associated_against: {
categories: [:label]
}
scope :since_date, -> (date) { where('created_at > ? OR updated_at > ? ', date, date) if date.present? }
scope :folder, -> { where.not(layout_id: ENV['ARTICLE_LAYOUT_ID']) }
scope :published_article, -> { published.article }
scope :article, -> { where(layout_id: ENV['ARTICLE_LAYOUT_ID']) }
阻止你可以处理异常而不是让它们崩溃你的程序!
您的代码来自try块,在catch块中,您可以处理从try块中执行代码抛出的Exception。
finally块用于关闭或完成已使用的资源,以便不打开它们。不这样做可能 - 具有讽刺意味 - 给你带来更多例外。
我肯定会看到它,它是现代语言中非常常用的部分!
答案 1 :(得分:0)
我想出了我需要做什么以及如何做。如果满足条件,我需要停止创建例程对象,并通过抛出错误来完成。这已经完成了,但我错误地将变量设置在try...catch
块中。因此,如果条件不满足,它将失败并停止,如果它们满足,它将创建对象并设置try...catch
块的变量OUTSIDE,这是有效的。我的新代码如下:
package routines;
import java.io.IOException;
import java.util.Random;
import game.*;
public class MovePlayer extends Routine {
private final int destX;
private final int destY;
private final Random random = new Random();
public MovePlayer(int destX, int destY, GameBoard board) throws IOException {
super();
try {
if (destY > board.getHeight() || destX > board.getWidth()) {
throw new IllegalArgumentException(">>> Error while creating routine, one or more coords are outside of the game board");
} else {
}
} catch (IllegalArgumentException e) {
fail();
System.err.println(e.getLocalizedMessage());
}
this.destX = destX;
this.destY = destY;
}