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 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You missed out the keyword new. Change your line to

 throw new InvalidDNAException("Invalid DNA String");

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.