我正在研究Java中的Sudoku求解器,作为有趣的语言介绍。我有我的代码做的事情之一是检查是否解决难题之前,它试图解决它。我认为这是一个好主意,使用try{}
catch{}
这个,但我不能得到编译代码。尝试在Java中捕获
public class SudokuSolver2
{
public static void main(String[] args) {
// 9x9 integer array with currentPuzzle
// check current puzzle and make sure it is a legal puzzle, else throw "illegal puzzle" error
try
{
// 1) check each row and make sure there is only 1 number per row
// 2) check each column and make sure there is only 1 number per column
// 3) check each square and make sure there is only 1 number per square
throw new illegalPuzzle("");
}
catch (illegalPuzzle(String e))
{
System.out.println("Illegal puzzle.");
System.exit(1);
}
}
}
public class illegalPuzzle extends Exception
{
public illegalPuzzle(String message)
{
super(message);
}
}
的问题,夫妇......
为什么不代码编译目前的形式?
有没有办法编写代码,以便我不必使用“String message”参数?我看到的所有示例都使用字符串参数,但我并不真正需要它或需要它。
有没有办法编写代码,以便我不必创建自己的自定义异常类?换句话说,我能抛出一个普遍的错误吗?我看到的所有示例都创建了自己的自定义异常,但我不需要那么详细。
谢谢!
类应以大写首字母来命名,这样反而IllegalPuzzle illegalPuzzle。 –