I've read that empty catch blocks are bad practice, but I have recently encountered a problem where one makes perfect sense. Since I am a beginner, I take this to mean there is a better solution to my problem.
I'm writing a routine to solve sudoku puzzles. This involves two algorithms: the first, simple_solve, which performs a variety of standard techniques to deduce the solution. (This alone works for easy to medium difficulty sudokus.) The second, guess_solve, which I am currently writing, involves making a guess and then running simple_solve to see if it works.
My goal here is to write the full solver on my own before I look at how other people do it, so please don't advise me on how to write sudoku solvers.
Here is the code snippet I wish to write next. (I hope my class and member functions are obvious from their names.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
int guess_solve(Sudoku& sudoku)
{
sudoku.simple_solve();
if(sudoku.solved()) // If simple solve works exit function.
return 0;
for([iterate over unsolved boxes b])
{
for([iterate over possible numbers i])
{
Sudoku s = sudoku;
s.guess(b,i);
try{
s.simple_solve(); // Simple solve throws if any box ends up
// with zero possibilities for its entry.
if(s.solved()) // If simple solve works exit function.
{
sudoku=s;
return 0;
}
else // If it gets stuck (but DOES NOT encounter
// an inconsistency) use recursion.
{
guess_solve(s);
sudoku=s;
return 0;
}
}
catch(...) // If our guess is wrong, simply move on
// and try the next guess.
{
}
}
}
}
|
I'm mainly interested in the usage of try/catch in this context rather than any minor mistakes (I haven't tested this yet).
Is this bad practice, and why?
Thanks.