简体   繁体   中英

Java exceptions to handle user inputs

Is it correct to respond to logically incorrect user inputs via exceptions ?

Here is an example:

We have many companies and each company has many employees. Each company has an id and each employee has an id. If user wants to delete employee from a specific company but the comapny he specified does not exist, is it okay to throw an exception like CompanyNotExtistsException ?

If I returned true or false I cant respond to user in GUI with the message why employee was not deleted - if he does not exist or if the company does not exist.

This is the topic of a lot of debate in the software community. There are those like @thatidiotguy who believe that an exception would be perfectly suitable in this case. And then there are others like me who believe exceptions should only be used for exceptional circumstances where something actually went wrong; your scenario strikes me as an alternate flow easily solved with a conditional.

But I am also not religious about this. If you go the exception route, just remember two things:

  • Make sure it is a checked exception as @thatidiotguy implied.
  • Make sure your exceptions don't duplicate existing exceptions provided by Java to say the same things. For example, if a client provides a string where you expect a number, don't make up your own "InvalidFormatException" or something. Use Java's built-in IllegalArgumentException .

Hope that helps.

Throwing exceptions is perfectly acceptable. There should be a try/catch block surrounding the call to the business side of things in your GUI code, so that you can display an error message if your business logic throws that exception.

eg

public class GUI {

     private UserManager manager;

     public void deleteUser(User user) {
         try{
             manager.deleteUser(user);  
         }
         catch(CompanyNotExistsException e) {
             //display an error message to user
         }
     }
}

public class UserManager {

    public void deleteUser(User user) throws CompanyNotExistsException {
           if(!this.companyExists(user.getCompany()) {
               throw new CompanyNotExistsException();
           }
    }
}

As per what @Vidya said below, I wish to clarify that I believe that there should be client-side validation so that this exception is thrown as a last resort. The coder should do all in his power to try and detect errors before they hit server-side code for performance and usability reasons.

It is not ok to throw an exception without actually doing anything about it (unless you don't plan on using it but others will in which case you document that so they know how to catch it). Now in your case throwing it when a company does not exist is fine, then catching it can easily let you know exactly what happened and you can "Fail Gracefully".

public void deleteEmployee(Employee e, Company c) throws CompanyNotExistsException{...}

then you catch it

try {
//do your work here
}
catch(CompanyNotExistsException e){/*Fail le graceful*/}
catch(Exception){/*For good measure*/}

Instead of throwing an exception, is there are way to prevent the erroneous state? In other words, is there any way to make them only be able to choose valid companies and valid employees in that company? As a user, I much prefer only being able to select valid entries instead of having to decipher what my error was. Even from a good error dialog. But that is your design decision to prevent or handle erroneous states.

Throwing exceptions is fine, as long as you inform the user what went wrong and don't exit unexpectedly. In one legacy GUI project I had, all UI-related exceptions had nice descriptions on the issue so I merely had to so them a JMessagePane (? forget exact class). In another, UI issue would be reported and the program (thus program session) would terminate and the user would need to start from scratch.

I think rather going for exception you can stop deleting and convey the message to the user. Say both company and employee information is in the database. So before deleting it is for sure that you have to check- whether company and employee exists Depending on the existence you can proceed deleting or not.

Steps-

if company exists then
   if employee  exists then 
     delete  
     message:= deleted
   else
     message:= employee doesn't exist
else
     message:= company doesn't exist

But you can use exception no restriction there. Like others shown using try-catch.

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