code_junkie

Java Style: Properly handling exceptions

Nov 13th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. public abstract class Data {
  2. public abstract String read();
  3. }
  4.  
  5. public abstract class Data {
  6. public abstract String read() throws DataUnavailableException;
  7. }
  8.  
  9. class DataFile extends Data {
  10. public String read() throws DataUnavailableException {
  11. if( !this.file.exits() ) {
  12. throw new DataUnavailableException( "Cannot read from ", file );
  13. }
  14.  
  15. try {
  16. ....
  17. } catch( IOException ioe ) {
  18. throw new DataUnavailableException( ioe );
  19. } finally {
  20. ...
  21. }
  22. }
  23.  
  24.  
  25. class DataMemory extends Data {
  26. public String read() {
  27. // Everything is performed in memory. No exception expected.
  28. }
  29. }
  30.  
  31. class DataWebService extends Data {
  32. public string read() throws DataUnavailableException {
  33. // connect to some internet service
  34. try {
  35. ...
  36. } catch( UnknownHostException uhe ) {
  37. throw new DataUnavailableException( uhe );
  38. }
  39. }
  40. }
Add Comment
Please, Sign In to add comment