RedDragonWebDesign
RedDragonWebDesign

Reputation: 2571

Try Throw Catch in Java

I'm working on a Sudoku solver in Java as a fun introduction to the language. One of the things I'm having my code do is check if the puzzle is solvable before it tries to solve it. I thought it would be a good idea to use try{} catch{} for this but I cannot get the code to compile.

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

Couple of questions...

  1. Why won't the code compile in its current form?

  2. Is there a way to write the code so that I do not have to use a "String message" parameter? All the examples I looked at use a string parameter but I don't really want it or need it.

  3. Is there a way to write the code so that I do not have to create my own custom exception class? In other words, can I just throw a general error? All the examples I looked at created their own custom exception but I don't need that level of detail.

Thanks!

Upvotes: 1

Views: 1019

Answers (3)

bgth
bgth

Reputation: 460

Please try to throw specific exception for the Illegal Puzzle Exception and catch other Exceptions in the other code block which might be thrown by other part of the code.

public static void main(String args[]) {
    try {
       throw new IllegalPuzzle("Illegal Puzzle");
    } catch ( IllegalPuzzle e ) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (Exception ex) {
        System.out.println("Inside Exception: " + ex.getMessage() );
    }
}

Also please look at below link before writing the throw code: Throwing exceptions in Java

Upvotes: 0

Rakesh KR
Rakesh KR

Reputation: 6527

Following figure show you the flow of a try catch block

enter image description here

try with,

try{
      throw new Exception("IllegalPuzzleException");
}catch (Exception e){
     System.out.println(e.getMessage());
}

Upvotes: 1

Aman Agnihotri
Aman Agnihotri

Reputation: 3023

Answer 1. The code won't compile in its current form, cause your catch clause should be written as follows:

catch (illegalPuzzle e)
{
  System.out.println("Illegal puzzle.");
  System.exit(1);
}

Answer 2. Just throw Exception (the base class of all Exceptions) in your try, and remove illegalPuzzle class altogether. Here's how:

public class SudokuSolver
{
  public static void main(String[] args)
  {
    try
    {
      // other statements
      throw new Exception();
    }
    catch (Exception e)
    {
      System.out.println("Illegal puzzle.");
      System.exit(1);
    }
  }
}

Answer 3. Answer 2 answers this part as well.

Upvotes: 2

Related Questions