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 Objects (Retired) Harnessing the Power of Objects Throwing Exceptions

Throwing an exception or throwing up....

what is the syntax or example of a throw method

throw(inserthere){};

? is that right.

GoKart.java
public class GoKart {
  public static final int MAX_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void drive() {
    drive(1);
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    throw(IllegalArgumentException){
      laps != 0
    }
    mBarsCount -= laps;
  }

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }

  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }

  public boolean isFullyCharged() {
    return mBarsCount == MAX_BARS;
  }

}

7 Answers

Inside your drive(int laps) method you need to add an if statement that checks if the laps is more than the remaining battery. if the laps > mBarsCount you will need to add a throw exception; it should look like this:

throw new IllegalArgumentException("Not enough battery remains");

is there a difference between throw and throws?

cant you do that with a try/catch block.

You could but the assignment doesn't call for us to do it that way.

if(mBarsCount < laps){
      throw new IllegalArgumentException("Not enough battery remains.");
    }

so that did work!!

Good deal!

what about this ? try/catch block doesn't work can you show me how it would work?

 public void drive(int laps) throws IllegalArgumentException {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
    try{
      if (mBarsCount < laps);
    } catch (IllegalArgumentException e){
       throw new IllegalArgumentException("Not enough battery remains.");
    }

  }

I actually misled you. the try/catch is more for runtime exceptions (errors). We actually threw an exception ourselves because the battery cannot have a negative charge; java doesn't think that way so it would have a negative charge which in reality isn't possible, so we had to send an error message to the user that they could not drive that many laps. We don't even have to use an exception we could have done something like this:

if(mBarsCount < laps){
System.out.println("Not enough battery remains");
System.exit(0);
}

It may not pass in the assignment because they are wanting us to throw an exception.

now a good reason to use a try/catch block would be to prevent someone from passing a letter as an argument when it is supposed to be a int (integer).

Like this:

try{
public void drive(int laps){
  if(mBarsCount < laps)
  throw new IllegalArgumentException("Not enough battery remains");
}
}catch(Exception e){
System.err.println("You can not pass letters as arguments!");
}

Im going to continue this thread for the next week this is a topic for my notes. :)

https://docs.oracle.com/javase/tutorial/essential/exceptions/ is what I am using to find exceptions to pass.