Created your exception class:

class ClassException extends IOException { public ClassException(String message) { super(message); } } 

In another class I try to forward an exception of this type:

 public void amountOfPayment() throws ClassException { System.out.println("Indicate the amount of monthly child support: "); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); totalPercentage(Double.parseDouble(reader.readLine())); } 

Swears on reader.readLine() :

Unhandled exception: java.io.IOException

What am I missing?

    2 answers 2

    The readLine method in the java.io.BufferedReader class throws an IOException that is parent (more general) than your ClassException . Therefore, it cannot be replaced with a more specific (your) in throws , because there are no guarantees that it will be the ClassException that is ClassException , and not the IOException itself or one of the other numerous exceptions inheriting from IOException .


    The situation is very similar to the following (perhaps it is easier to understand). Creating your own class that extends Number :

     class MyNumber extends Number { ... } 

    and an incorrect use attempt with an object that is Number ( class Integer extends Number ), but not MyNumber :

     public MyNumber get() { Integer n = 0; return n; } 

    One of the possible solutions in this case (by analogy with the answer @SergeyGornostaev) is to create in MyNumber constructor that takes a Number :

     class MyNumber extends Number { public MyNumber(Number number) { ... } ... } 

    and returning from the get method a new MyNumber object:

     public MyNumber get() { Integer n = 0; return new MyNumber(n); } 

      The reasons correctly and in detail described Regent. The solution may be:

       public void amountOfPayment() throws ClassException { System.out.println("Indicate the amount of monthly child support: "); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { totalPercentage(Double.parseDouble(reader.readLine())); } catch (IOException exc) { ClassException myExc = new ClassException(exc.getMessage()); myExc.initCause(ioExc); throw myExc(); } } 

      Or easier, if you override the constructor that accepts Throwable :

       class ClassException extends IOException { public ClassException(String message) { super(message); } public ClassException(Throwable exc) { super(exc); } } try { totalPercentage(Double.parseDouble(reader.readLine())); } catch (IOException exc) { throw new ClassException(exc); } 
      • I otdebazhil and see that when an exception is thrown in a call, it doesn't even enter ClassException - Aleksandr
      • Corrected the answer. - Sergey Gornostaev
      • When I get an exception, the program immediately displays the trace and ends, and how not to output stacktrace, but output its error message? - Aleksandr
      • When I get an exception, the program immediately displays the trace and ends, and how not to output stacktrace, but output its error message? - Aleksandr
      • In the code that amountOfPayment intercept a ClassException exception. - Sergey Gornostaev