I am trying to include some exceptions in my code but there is something that I am missing as the compiler is complaining. I would be grateful if someone could point out what the problem is.
I have a class called InvalidSequenceException which is saved in a separate file from my other class files but in the same directory.
This is supposed to return the content string up until the invalid character and the index of that character.
I then have a method within my Sequence class which validates the characters and is able to throw this exception. The loop that is used to test for valid works as it will printout if the character is invalid.
The method validate is its self setup in the constructor of the Sequence class.
The aim of this is so that only valid sequences are created and any with invalid characters are supposed to be handled by catching and reporting the exception.
So in main I have setup a test to check that the sequence is valid
I had thought that this should work but I get the following error
---------- Capture Output ---------- > "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" Sequence.java Sequence.java:36: cannot find symbol symbol : constructor InvalidSequenceException() location: class InvalidSequenceException throw new InvalidSequenceException(); ^ 1 error
> Terminated with exit code 1.
I thought that if the method was defined as one that throws an exception then a new exception could be created using throw new exception. The line the compiler does not like is this very one
throw new InvalidSequenceException();
Hopefully someone can explain why this is not working.
Two very minor things are wrong here; your understanding of everything seems just fine.
The compiler is telling you that you didn't give the InvalidSequenceException class a no-argument constructor, which is quite true: you gave it a constructor which accepts a String and an int. When you construct one to throw, you'll need to supply these arguments, something like
throw new InvalidSequenceException(content, i);
Finally, the Sequence constructor calls validate, and you want that constructor to fail if the exception is thrown; so that constructor must also declare that it "throws InvalidSequenceException" as well.