How to return status code 500 if there is an IOException during reading

Say I have the following snippet

public boolean checkListing() { try { // open the users.txt file BufferedReader br = new BufferedReader(new FileReader("users.txt")); String line = null; while ((line = br.readLine()) != null) { String[] values = line.split(" "); // only interested for duplicate usernames if (username.equals(values[0])) { return true; } } br.close(); return false; } catch (IOException e) { e.printStackTrace(); // what to do here } } 

How do I handle an error if an exception occurs? I want to know what happened, and return the 500 code back to the user.

Should I throw and exclude and catch him in another class?

Is there a more elegant way to get feedback?

+5
source share
4 answers

You can return an instance of this class:

 public class Result { private boolean errorOccurs; private boolean isValid; private Exception exception; public Result(boolean isValid){ this(isValid, false, null); } public Result(boolean isValid, boolean errorOccurs, Exception exception){ this.isValid = isValid; this.errorOccurs = errorOccurs; this.exception = exception; } public boolean isValid(){ return isValid; } public boolean errorOccurs(){ return errorOccurs; } public Exception getException(){ return exception; } } 

In your case:

 public Result checkListing() { try { // open the users.txt file BufferedReader br = new BufferedReader(new FileReader("users.txt")); String line = null; while ((line = br.readLine()) != null) { String[] values = line.split(" "); // only interested for duplicate usernames if (username.equals(values[0])) { return new Result(true); } } br.close(); return new Result(false); } catch (IOException e) { e.printStackTrace(); return new Result(false, true, e); } } 

And the short form of the Result class :)

 public class Result { public boolean errorOccurs; public boolean isValid; public Exception exception; } 
+1
source

In this case, an IOException can be selected from readLine or close .

This exception indicates that an exception has occurred, for example, the stream source is no longer available. Your program can be restored or re-read the source or report the problem to the user.

It is up to you to decide what to do, depending on your logic, there is no preferred way to do this.

Note that you must close br in the finally block.

0
source

The best way is to exclude the shell exception:

 try (FileReader fr = new FileReader("users.txt")) { try (BufferedReader br = new BufferedReader(fr)) { // ... do your work } catch (IOException e) { throw new RuntimeException(e); } } catch (IOException e1) { throw new RuntimeException(e1); } 

Please note that here I open resources in try , which makes them automatically closed at the end of execution, regardless of the result of the execution.

0
source

The two most common approaches I have seen are, as Sasha Salauu pointed out, a retroment (potentially wrapped) or a separate object that provides additional information.

The main problem you are trying to deal with is that you have a method that you want to return more than two values ​​with a return type that can only express two. Thus, you either need to return a type that supports all possible return values ​​that you need, or you need to find alternative return paths (for example, exceptions).

In my own code, I would lean towards a revolver here. Writing a highlighted return object becomes difficult to maintain in the long run with I / O, because there are so many different ways that things can fail. However, I would deviate from the proposed route (an exception containing the original exception as a reason) simply because there is usually no good reason to wrap it up. Just declare your checkListing method to throw an IOException, register your message or stacktrace in the catch block, and then throw the original exception.

0
source

All Articles