简体   繁体   中英

Exception handling when calling a method

Say i have one method 'position', which takes 2 co-ordinates and creates a position object to hold them in. To make sure that these co-ordinates are not out of bounds, an InvalidPositionException is thrown.

public Position(int x, int y) throws InvalidPositionException {
    try {
        if (x > 10 || x < 1 || y>10 || y<1) {   
            throw new InvalidPositionException("Position x = "+x+", y = "+y+" is out of bounds");
        } else {
            setX(x);
            setY(y);
        }
    } catch (InvalidPositionException e) {
        System.err.println(e);
    }
}

If I now want to create a new position object from another class, I get the error message "unreported exception InvalidPositionException; must be caught or declared to be thrown"

How can I make this work without declaring "throws" in the method signature of the method creating the new position object?

Go to the class InvalidPositionException and make it inherit from RuntimeException .

RuntimeException s are unchecked exceptions, meaning that the compiler does not force you to deal with them. A good example of this when you go out of bounds from your array. You don't have to write code that deals with this issue every time you access an array (like how we handle IOException ). A similar example is NullPointerException . In both cases, however you could write code to catch the exception and handle it.

Another way to deal with the problem if you are a checked exceptions purist and don't want a try-catch block is to sanitise the data in whatever screen you input the data and say that it is out of bounds there.

From Effective Java by Joshua Bloch

Item 72: Favor the use of standard exceptions

An attribute that distinguishes expert programmers from less experienced ones is that experts strive for and usually achieve a high degree of code reuse. Exceptions are no exception to the rule that code reuse is a good thing. The Java libraries provide a set of exceptions that covers most of the exception-throwing needs of most APIs. ...

IllegalArgumentException occasions for use - Non-null parameter value is inappropriate

This code is a classic example the IllegalArgumentException was created for. It extends RuntimeException so you are not required to catch it.

public Position(int x, int y) {
    if (x > 10 || x < 1 || y>10 || y<1) {   
        throw new IllegalArgumentException("Position x = "+x+", y = "+y+" is out of bounds");
    } 
    setX(x);
    setY(y);
}

PS you probably can just replace setX(x) by this.x = x and the same for y (if there's no extra logic in those setters).

You need to include a try-catch block around the statement which creates the Position object.

Position myPosition = null;
try {
  myPosition = new Position(a, b);
}
catch (InvalidPositionException e) {
  // do something if the exception is caught
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM