0

I'm getting this error: "unreported exception IOException; must be caught or declared to be thrown" in this piece of code:

 a = Double.parseDouble ( kb.readLine () ) ;
 b = Double.parseDouble ( kb.readLine () ) ;
 c = Double.parseDouble ( kb.readLine () ); 

Here is the rest of the code:

 package quadratic.java;


/**
 *
 * @author Painfulmono
 */
 import java.io.*;
 public class QuadraticDriver {

     BufferedReader kb = new BufferedReader 
                    (new InputStreamReader (System.in));
 public static void main (String [] args){

     double a, b, c;

     a = Double.parseDouble ( kb.readLine () ) ;
     b = Double.parseDouble ( kb.readLine () ) ;
     c = Double.parseDouble ( kb.readLine () ); 

     QuadraticJava q1 = new QuadraticJava (a, b, c);

     q1.solve();
}

}

How would I get it declared?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Painfulmono
  • 43
  • 1
  • 1
  • 5
  • 1
    Which kind of input are you sending to the console? – Marco Salerno Mar 13 '17 at 17:29
  • 1
    To "declare" an exception you add a throws clause to your method signature, e.g. `throws IOException`. But you probably might to use a try-catch block instead: `try{ /*your code*/ } catch( IOException e) { /*handle the exception*/ }`. – Thomas Mar 13 '17 at 17:29

2 Answers2

4

readLine() declares that it throws an IOException. This is a checked exception, meaning you can't just ignore it. You need to either catch it, e.g.:

try {
    a = Double.parseDouble ( kb.readLine () ) ;
} catch (IOException e) {
    System.out.println("Can't read a"); // Or something more intellegent
}

Or, if you don't have any intelligent way to handle it, you could just throw it upwards by adding this exception to the caller's signature, e.g.:

public static void main (String [] args) throws IOException {
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Note that if you throw from `main(String[] args)`, your application will likely hang or crash because the execution pointer follows the exception handling path and you have undefined control "above" main(). – geneSummons Mar 13 '17 at 17:39
  • 1
    @geneSummons if you throw an exception from `main`, the program should terminate – Mureinik Mar 13 '17 at 19:17
2

In Java, when dealing with Exceptions, it is a rule of thumb that you must either Handle(catch) the exception OR just Declare it.

In your case, its better to catch(handle) the exception by wrapping your code in the try block. You should not go for the Declare part of the rule because when you declare that somemethodA() throws XYZException then there should be some other method down the call stack which handles/declares this XYZException.
Here, you must not declare main method to throw the IOException, becuase if main starts throwing exception, then nothing catches it as the main() method sits at the bottom of the call stack.

The BufferedReader's readLine() method used in your code throws the IOException. You should use the try/catch wherever readLine() method is used to make way for graceful exit of the program in case any IOExeption does actually occur.

Avantol13
  • 1,009
  • 11
  • 21
Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47