0

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.

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks :) I will handle the input parameters mistakes (like String value instead of integer and so on) via regexes in GUI. But I found it as a bigger complication to use an int value as the return of my method to identify where the problem has occured.
+1 for the other side of the opinion. Its important to really know when to use exceptions.
I do wish to clarify that I would always include user-side validation so that the exception is not the primary means of alerting the GUI to an invalid state. I will add that to my answer.
1

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.

e.g.

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.

1 Comment

thanks for your answer ... I was thinking of using integer as return value but this would complicate the code above in GUI. But I wasnt sure if this is the case when it is good to use an exception because I read about the fact that exceptions should not be used to control the program flow
1

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*/}

1 Comment

thanks very much :) I actually will handle all exceptions in GUI
0

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.

2 Comments

thanks for your answer :) im going to design the front end as you say - the user can choose only from companies that extist. But I want to write my controllers to be error resistant in general way. So if somebody will change the gui into the bad form as you write, my controllers will work wihtout any crashes
If the user can "correct" the error, then throw the exception. I don't think anyone can code around any possible input from a user.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.