File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
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.

Best wishes
Dianne Gerrelli
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The constructor you've defined for InvalidSequenceException takes a String and an int. Note that if you provide any constructor, the compiler will not provide a default constructor.

So unless you also define a no-args constructor, you need to pass the required arguments when you create a new instance of the exception.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Dianne,

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.


[Jess in Action][AskingGoodQuestions]
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks very much for the replies. I think I understand exception handling a little better know.

Best wishes
Dianne Gerrelli
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions