title: How can I throw a general exception in Java?
body: <p>Consider this simple program. The program has two files:</p>
  <h3>File <em>Vehicle.java</em></h3>
  <pre><code>class Vehicle {
      private int speed = 0;
      private int maxSpeed = 100;
  
      public int getSpeed()
      {
          return speed;
      }
  
      public int getMaxSpeed()
      {
          return maxSpeed;
      }
  
      public void speedUp(int increment)
      {
          if(speed + increment &gt; maxSpeed){
              // Throw exception
          }else{
              speed += increment;
          }
      }
  
      public void speedDown(int decrement)
      {
          if(speed - decrement &lt; 0){
              // Throw exception
          }else{
              speed -= decrement;
          }
      }
  }
  </code></pre>
  <h3>File <em>HelloWorld.java</em></h3>
  <pre><code>public class HelloWorld {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
  
          Vehicle v1 = new Vehicle();
          Vehicle v2 = new Vehicle();
  
              // Do something
  
              // Print something useful, TODO
          System.out.println(v1.getSpeed());
      }
  
  }
  </code></pre>
  <p>As you can see in the first class, I have added a comment (&quot;// throw exception&quot;) where I would like to throw an exception. Do I have to define my own class for exceptions or is there some general exception class in Java I can use?</p>