Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. // Try commenting in and out different lines below
  2. // to see how Exceptions are handled.
  3. public class ExceptionTest {
  4.  
  5. // Exceptions are just instances of the Exception class
  6. // or any subclass... we can make our own Exception subclasses
  7. // for Exceptions specific to a particular application.
  8. public static class LocalException extends Exception {
  9. public LocalException(String msg) { super(msg); }
  10. }
  11.  
  12. public static double A() throws Exception {
  13. return B();
  14. }
  15.  
  16. public static double B() throws Exception {
  17. double return_val = 50d;
  18. try {
  19. return_val = C();
  20. } catch (LocalException e) {
  21. return_val = 100d;
  22. //throw e;
  23. //throw new Exception("Modified Exception: " + e.getMessage());
  24. } finally {
  25. System.out.println("Always printed!");
  26. }
  27. return return_val;
  28. }
  29.  
  30. // Try each of the following three return statements
  31. public static double C() throws Exception {
  32. throw new Exception("ERROR");
  33. //throw new LocalException("ERROR");
  34. //return 1.0;
  35. }
  36.  
  37. public static void main(String[] args) {
  38. double val = 3d;
  39. try {
  40. val = A();
  41. System.out.println("Main Value: " + val);
  42. } catch (Exception e) {
  43. System.out.println("Main Exception: " + e.getMessage());
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement