How to make a general exception in Java?

Consider this simple program. The program has two files:

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

And 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 added a comment ("// throw exception"), where I would like to make an exception. Should I define my own class for exceptions, or is there any general Java exception class that I can use?

+60
java exception
Aug 04 2018-11-11T00:
source share
8 answers

You can create your own Exception class:

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

In your code:

 throw new InvalidSpeedException("TOO HIGH"); 
+95
Aug 04 2018-11-11T00:
source share

You can 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; } } 
+40
Aug 04 2018-11-11T00:
source share

Well, there are many throw exceptions, but here's how you make an exception:

 throw new IllegalArgumentException("INVALID"); 

Also yes, you can create your own custom exceptions.

Edit: Also note the exceptions. When you throw an exception (like the one above), and you find the exception: the String that you provide in the exception can be accessed by the getMessage() method.

 try{ methodThatThrowsException(); }catch(IllegalArgumentException e) { e.getMessage(); } 
+13
Aug 04 2018-11-11T00:
source share

It really depends on what you want to do with this exception after you catch it. If you need to distinguish your exception, you need to create your own Exception . Otherwise, you could simply throw new Exception("message goes here");

+5
Aug 4 2018-11-11T00:
source share

The easiest way to do this:

 throw new java.lang.Exception(); 

However, the following lines would not be achievable in your code. So, we have two ways:




  • throw a general exception at the bottom of the method. \
  • create your own exception if you don't want to do 1.
+5
Nov 30 '16 at 11:14
source share

Java has a large number of built-in exceptions for different scenarios.

In this case, you should throw an IllegalArgumentException , since the problem is that the caller passed a bad parameter.

+3
Aug 04 2018-11-11T00:
source share

You can define your own exception class extending java.lang.Exception (which should be caught for a checked exception) or extending java.lang.RuntimeException - these exceptions should not be caught. Another solution is to review the Java API and find an appropriate exception that describes your situation: in this particular case, I believe that an IllegalArgumentException would be the best.

+1
04 Aug. 2018-11-11T00:
source share

It depends, you can throw a more general exception or a more specific exception. For simpler methods, general exceptions are sufficient. If the method is complex, then throwing a more specific exception will be reliable.

+1
Aug 04 2018-11-11T00:
source share



All Articles