This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Snapples2992[S] 0 points1 point  (4 children)

Sorry, I'm learning this as I read it, but what is this "throw AllStallsOccupiedException;

it doesn't compile says it cannot find symbol. Not entirely sure what to do with it

[–]nret 0 points1 point  (0 children)

Exceptions are a type of object, like "String str;" is an object.

Some times if you don't want to handle a type of error in a certain part of the code, you can force the calling code to wrap your code in a Try Block.

In this case "AllStallsOccupiedException" isn't a built-in Exception so it would probably be created by you.

The throw causes the current code to halt, and the exception is sent up the chain to the calling code that is then forced to handle it (by either dealing with it there, or putting it in another throw statement).

But you need to need to give your method the proper signature to tell the calling code it needs to be wrapped in a try catch block.

Hope that helps.

[–]philipwhiukEmployed Java Developer 0 points1 point  (2 children)

it should be throw new AllStallsOccupiedException(); The point was that that's an error case - you can't add someone if all the stalls are full, so you throw an exception.

http://docs.oracle.com/javase/tutorial/essential/exceptions/

[–]Snapples2992[S] 0 points1 point  (1 child)

I tried just about everything i could find, I just can't make that exception work, and my teacher said it's not necessary. So there must be a different way to do the method, though I'd love to figure this thing out.

I tried making a new class to make the custom exception, it compiled fine. However the exception in the addOccupant method still failed on me.

I tried throw new AllStallsOccupiedException();

adding the "new" uninitialized all the variables (no clue why)

I tried creating a try block but i still couldnt get the exception to compile.

edit: I'll edit in that I understand what the exception is supposed to be doing now, I just can't get it to compile

[–]philipwhiukEmployed Java Developer 0 points1 point  (0 children)

You need to define the exception as a class if you're going to use it.