Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I throw a general exception in Java?

Tags:

java

exception

Consider this simple program. The program has two files:

File Vehicle.java

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 > maxSpeed){             // Throw exception         }else{             speed += increment;         }     }      public void speedDown(int decrement)     {         if(speed - decrement < 0){             // Throw exception         }else{             speed -= decrement;         }     } } 

File HelloWorld.java

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());     }  } 

As you can see in the first class, I have added a comment ("// throw exception") 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?

like image 487
Richard Knop Avatar asked Aug 04 '11 13:08

Richard Knop


People also ask

What is general exception in Java?

As you've seen, Java offers you two general types of exceptions: The checked and the unchecked exception. You should use a checked exception for all exceptional events that can be expected and handled by the application. You need to decide if you want to handle it within a method or if you specify it.

How does Java handle general exception?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

How do you throw an exception value in Java?

How to throw an exception. To throw an exception, we generally use the throw keyword followed by a newly constructed exception object (exceptions are themselves objects in Java). Most exception constructors will take a String parameter indicating a diagnostic message.


2 Answers

You could create your own Exception class:

public class InvalidSpeedException extends Exception {    public InvalidSpeedException(String message){      super(message);   }  } 

In your code:

throw new InvalidSpeedException("TOO HIGH"); 
like image 60
Fortega Avatar answered Sep 20 '22 22:09

Fortega


You could use IllegalArgumentException:

public void speedDown(int decrement) {     if(speed - decrement < 0){         throw new IllegalArgumentException("Final speed can not be less than zero");     }else{         speed -= decrement;     } } 
like image 39
Maurício Linhares Avatar answered Sep 18 '22 22:09

Maurício Linhares