code_junkie

Use continue or Checked Exceptions when checking and processing objects

Nov 11th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. for (Document document : List<Document> documents) {
  2. if (!fileIsPresent(document)) {
  3. doSomethingWithThisResult("File is not present");
  4. continue;
  5. }
  6. if (!isSomethingInTheDocumentPresent(document)) {
  7. doSomethingWithThisResult("Something is not in the document");
  8. continue;
  9. }
  10. doSomethingWithTheSucces();
  11. }
  12.  
  13. for (Document document : List<Document> documents) {
  14. try {
  15. fileIsPresent(document);
  16. isSomethingInTheDocumentPresent(document);
  17. doSomethingWithTheSucces();
  18. } catch (ProcessingException e) {
  19. doSomethingWithTheExceptionalCase(e.getMessage());
  20. }
  21. }
  22.  
  23. public boolean fileIsPresent(Document document) throws ProcessingException {
  24. ... throw new ProcessingException("File is not present");
  25. }
  26.  
  27. public boolean isSomethingInTheDocumentPresent(Document document) throws ProcessingException {
  28. ... throw new ProcessingException("Something is not in the document");
  29. }
  30.  
  31. for (Document document : List<Document> documents) {
  32. if(checkDocument(document)) {
  33. doSomethingWithTheSucces();
  34. } else {
  35. doSomethingWithTheError();
  36. }
  37. }
  38.  
  39. // Later:
  40. public boolean checkDocument(Document doc) { ... }
  41.  
  42. for (Document document : List<Document> documents) {
  43. try {
  44. checkDocument(document);
  45. doSomethingWithTheSucces();
  46. } catch(CheckDocumentException ex) {
  47. doSomethingWithTheError(ex);
  48. }
  49. }
  50.  
  51. // Later:
  52. public void checkDocument(Document doc) throws CheckDocumentException { ... }
Add Comment
Please, Sign In to add comment