Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - ignore exception and continue

Tags:

java

For my Java application, I am creating an instance of a user information object and populating it with a service that I don't control the source for.

The code looks like this:

// username given as parameter
UserInfo ui = new UserInfo();
try {
    DirectoryUser du = LDAPService.findUser(username);
    if (du!=null) {
       ui.setUserInfo(du.getUserInfo());
    }
} catch (Exception e) {
    // Whatever
}

If LDAPService.findUser() can't locate a user, it will throw a NullPointerException and grind the rest of my application to a stop. It's okay if the user information isn't populated, so I want to be able to continue without causing everything else to start throwing exceptions.

Is there a way to do this?

like image 678
SpeedBurner Avatar asked Dec 08 '11 19:12

SpeedBurner


People also ask

How do I ignore an exception in Java?

To ignore an exception in Java, you need to add the try... catch block to the code that can throw an exception, but you don't need to write anything inside the catch block.

How do you continue after throwing an exception?

Resuming the program When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after execution of the complete program.

How do I ignore NullPointerException?

Answer: Some of the best practices to avoid NullPointerException are: Use equals() and equalsIgnoreCase() method with String literal instead of using it on the unknown object that can be null. Use valueOf() instead of toString() ; and both return the same result.

Which keyword is used to ignore the exception from any method?

In the catch block you can mention the exception and ignore keyword for that exception.


2 Answers

I've upvoted Amir Afghani's answer, which seems to be the only one as of yet that actually answers the question.

But I would have written it like this instead:

UserInfo ui = new UserInfo();

DirectoryUser du = null;
try {
    du = LDAPService.findUser(username);
} catch (NullPointerException npe) {
    // It's fine if findUser throws a NPE
}
if (du != null) {
   ui.setUserInfo(du.getUserInfo());
}

Of course, it depends on whether or not you want to catch NPEs from the ui.setUserInfo() and du.getUserInfo() calls.

like image 58
Zecc Avatar answered Nov 07 '22 01:11

Zecc


You could catch the NullPointerException explicitly and ignore it - though its generally not recommended. You should not, however, ignore all exceptions as you're currently doing.

UserInfo ui = new UserInfo();
try {
    DirectoryUser du = LDAPService.findUser(username);
    if (du!=null) {
       ui.setUserInfo(du.getUserInfo());
    }
} catch (NullPointerException npe) {
    // Lulz @ your NPE
    Logger.log("No user info for " +username+ ", will find some way to cope");
}
like image 23
Amir Afghani Avatar answered Nov 07 '22 00:11

Amir Afghani