/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java

https://bitbucket.org/nbargnesi/idea · Java · 120 lines · 104 code · 16 blank · 0 comment · 6 complexity · 8da5db0f03461b33fa44eaeb872b899e MD5 · raw file

  1. package com.siyeh.igtest.errorhandling.try_identical_catches;
  2. class TryIdenticalCatches {
  3. public void notIdentical() {
  4. try {
  5. }
  6. catch(NumberFormatException e) {
  7. log(e);
  8. }
  9. catch(RuntimeException e) {
  10. throw e;
  11. }
  12. }
  13. String nonIdenticalButDuplicated(Object o) {
  14. try {
  15. } catch (NullPointerException e) {
  16. if (o instanceof String) {
  17. return a((String) o);
  18. }
  19. } catch (NumberFormatException e) {
  20. if (o instanceof String) {
  21. return b((String) o);
  22. }
  23. }
  24. return null;
  25. }
  26. String a(String s) { return s;}
  27. String b(String s) { return s;}
  28. public void nonIdenticalWithParameterValue() throws StorageException {
  29. try {
  30. throwAllExceptions();
  31. }
  32. catch (StorageInitializationException e) {
  33. throw e;
  34. }
  35. catch (java.io.IOException e) {
  36. throw new StorageInitializationException("Can not setup storage factory.", e);
  37. }
  38. catch (Exception e) {
  39. throw new StorageInitializationException("Unspecified exception occurs while DB storage initialization.", e);
  40. }
  41. }
  42. void throwAllExceptions() throws StorageInitializationException, java.io.IOException {
  43. }
  44. class StorageException extends Exception {
  45. }
  46. class StorageInitializationException extends StorageException {
  47. private StorageInitializationException(String m, Exception e) {
  48. }
  49. }
  50. public void identicalWithoutParams(boolean value) {
  51. try {
  52. if (value) {
  53. throw new ClassNotFoundException();
  54. }
  55. else {
  56. throw new NumberFormatException();
  57. }
  58. }
  59. catch(ClassNotFoundException cnfe) {
  60. System.out.println();
  61. }
  62. catch(NumberFormatException nfe) {
  63. System.out.println();
  64. }
  65. }
  66. public void identical(boolean value) {
  67. try {
  68. if (value) {
  69. throw new ClassNotFoundException();
  70. }
  71. else {
  72. throw new NumberFormatException();
  73. }
  74. }
  75. catch(ClassNotFoundException | NumberFormatException cnfe) {
  76. log(cnfe);
  77. }
  78. }
  79. private void log(Exception e) {
  80. }
  81. class E1 extends RuntimeException {}
  82. class E2 extends E1 {}
  83. class E3 extends RuntimeException {}
  84. class E4 extends E3 {}
  85. void p() {
  86. try {
  87. } catch (E4 e) {
  88. } catch (E2 e) {
  89. } catch (E3 e) {
  90. } catch (E1 e) {
  91. }
  92. }
  93. void q() {
  94. try {
  95. Class.forName("bla").newInstance();
  96. } catch (ClassNotFoundException e) {
  97. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  98. } catch (InstantiationException ) {
  99. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  100. } catch (IllegalAccessException e) {
  101. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  102. }
  103. }
  104. }