• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for the replies. I think I understand exception handling a little better know.

Best wishes
Dianne Gerrelli
reply
    Bookmark Topic Watch Topic
  • New Topic