-5

I'm having a hard time figuring out how to throw and catch exceptions.
I Think I have the general syntax right, at least according to the book that I'm using, but I keep getting errors.

Can anyone help me out with the syntax?

public Movie( String title, int yearReleased, double rating){
    try{
        if (rating < 4.1 && rating >= 0.0)
            this.rating = rating; 
        else
            throw new IllegalArgumentException;
    catch (IllegalArgumentException e){
        System.out.println("This rating is invalid");
    }
}
csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63

3 Answers3

3

You have two issues:

1) Your closing } for the try is inside the if.

2) You are not properly calling the constructor of IllegalArgumentException. Do `throw new IllegalArgumentException();

In the future, you should read and try to understand the compiler message. It is most likely telling you exactly what there errors are and where. (That is why your question has been down-voted so much.)

I get that you're new to this so I am more than happy to help you out.

Happy coding.

Todd
  • 3,438
  • 1
  • 27
  • 36
  • whoops sorry! edited to get rid of that extra bracket, I'll try your suggestion right now –  May 08 '14 at 14:20
  • 1
    @maribov it doesn't really make sense to post a question, wait for answers to that question, then edit the core of the question now does it? – Boris the Spider May 08 '14 at 14:20
  • 1
    My advice is to learn to read compiler messages. You have to do that if you want to get good with Java or any compiled language. – Todd May 08 '14 at 14:21
  • that wasn't the core of the question, the second part of @todd's answer was the core of the question :) –  May 08 '14 at 14:22
  • the first part was just a typo hehehe –  May 08 '14 at 14:22
  • @Todd all that it was telling me was that "IllegalArgumentException" could not be assigned to a variable in the compiler message –  May 08 '14 at 14:23
  • @maribov Now that you have it working. Put the syntax error back and checkout the compiler error again. That way you recognize it better next time. – Todd May 08 '14 at 14:26
0

The mistakes we're doing while not enclosing brackets..

You have a redundant closing brackets.

this.rating = rating; }

Please remove it.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

try

throw new ArgumentException();

because it is constructor and you forgot to put parentheses

akhilless
  • 3,169
  • 19
  • 24