0

So I programmed this exception for a lab I'm doing:

public class InvalidDNAException extends Exception{
   public InvalidDNAException(String message){
      super(message);
       }
}

and then tried to put the exception into the class I'm using for the lab

try { for (int i=0;i<nucleo.length();i++){
        //code to be implemented later
        else throw InvalidDNAException("Invalid DNA String");
      }
}
catch(InvalidDNAException e){
         System.out.print("Invalid string");
         System.exit(0);
}

I keep getting the error:

cannot find symbol
     else throw InvalidDNAException("Invalid DNA String");
                ^
  symbol:   method InvalidDNAException(String)
  location: class DNA

Did I just not create the exception correctly or what can I do to fix this?

3 Answers3

2

You forgot new:

else throw new InvalidDNAException("Invalid DNA String");
//         ^^^ this is important

Also, you shouldn't just throw an exception only to catch it and System.exit in the same method. If you're not going to write the code to deal with a checked exception properly, at least wrap it in an unchecked exception so you get the stack trace:

catch (InvalidDNAException e) {
         // You really ought to do something better than this.
         throw new RuntimeException(e);
}
user2357112
  • 260,549
  • 28
  • 431
  • 505
0

You missed out the keyword new. Change your line to

 throw new InvalidDNAException("Invalid DNA String");
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24
0

throw InvalidDNAException("Invalid DNA String"); is not a valid syntax

InvalidDNAException is a class and you need to call the constructor by doing:

throw new InvalidDNAException("Invalid DNA String");
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97