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

Java

java

Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be completed on the current battery level. Make the exception message "Not enough battery remains".

public void drive(int laps) { // Other driving code omitted for clarity purposes int totalLaps = mBarsCount -= laps; if(totalLaps > mBarsCount){ throw new IllegalArgumentException( "Not enough battery remains"); } mBarsCount = totalLaps; }

1 Answer

Make sure you post link to challenge, of hit Get Help button, to properly paste link to challenge, that helps a lot :) In order to find challenge :)

Now back to your code

public void drive(int laps) { 
  // Other driving code omitted for clarity purposes 
  // int totalLaps = mBarsCount -= laps; // this line is incorrect... what did you want to achieve with it ?
  if(totalLaps > mBarsCount){ 
    throw new IllegalArgumentException( "Not enough battery remains"); 
  } 
  // mBarsCount = totalLaps;  incorrect
  // here you have to leave the code before: simple -= decrementing
}

Does it make more sense now ? :)

So all you need to do is to add if statement just like you did without changing anything else in this code. Very simple, just like that.

Is that your challenge BTW?

https://teamtreehouse.com/library/java-objects/harnessing-the-power-of-objects/throwing-exceptions

?