Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Allen C
1,448 PointsSo what does "throw" do?
What does it do here? Furthermore, why does "new" have to be used?
public void load(int pezAmount) {
int newAmount = mPezCount + pezAmount;
if (newAmount > MAX_PEZ) {
throw new IllegalArgumentException("Too many Pez!!!");
The other piece of code that used this:
try {
dispenser.load(400);
} catch(IllegalArgumentException iae) {
System.out.println("Nope failing!");
System.out.printf("The error was: %s\n", iae.getMessage());
}
Is it simply implementing a string that could be printed if this Exception was to occur?
1 Answer
Stone Preston
42,016 PointsEssentially exceptions are thrown and caught. The throw keyword "throws" that exception. It causes an error that stops execution of the program.
The new keyword is used because exceptions are objects, and therefore in order to create a new exception object it must be instantiated using the new keyword along with the constructor for that Class
Whenever you are working with code that could possibly throw an exception, you can use the try and catch block.
If for some reason an exception is thrown, it will be "caught" by the catch block and you can handle the error how you see fit. By handling the exception inside the catch block, you can print out necessary info and keep the program from straight up crashing
If you did not use the try/catch block, the exception would be thrown, it would not be caught, and it would cause an error that forces execution to stop (this is NOT good, you generally want to be defensive and catch exceptions if they occur)
Seph Cordovano
17,400 PointsSeph Cordovano
17,400 PointsYou were right, I wasn't thinking clearly about that.
Allen C
1,448 PointsAllen C
1,448 PointsWhy is it that in another example code I saw on youtube, the video did not have to use throw, but rather, used the name "Exception" rather than "IllegalArgumentException"? How come he didn't have to throw it as a new object?
Seph Cordovano
17,400 PointsSeph Cordovano
17,400 PointsAllen C, that's because all exceptions stem from the Exception class, whether they're built in or custom exceptions. Because of that throwing Exception can be used sort of a catch-all. Often times you'll use a try-catch that has multiple catches so that you can do different actions based on what exceptions is thrown. A try-catch catches the first exception it finds that works so Exception is usually the last one caught. ie: