-2

I know Exceptions are occurs at run time not at the time of compilation.

so this code compiles successfully without any compile time Exception of type java.lang.ArithmeticException: / by zero but give an exception only at the run time

class divzero{
public static void main(String[] arg){
System.out.print(3/0);
}
}

but when i am using BufferedReader class it saying "unreported exception java.io.IOException; must be caught or declared to be thrown" when i am compiling the code given below. I think it should produce this exception at run time not at the time of compilation.because exceptions occurs at compile time.

import java.io.*;
class Bufferedreaderclass{

public static void main(String[] arg)
{
System.out.print("mazic of buffer reader \n Input : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
input = br.readLine();
System.out.print("Your input is: "+input);
}
}

I know, i should throw an exception of IOException type but why? i want to know why "unreported exception java.io.IOException" at compile time not at run time.

please make it clear to me why it is behaving like this ?

CoderNeji
  • 2,056
  • 3
  • 20
  • 31
g1ji
  • 1,099
  • 1
  • 10
  • 21

2 Answers2

2

Since IOException is a checked exception, you must use either try...catch or 'throws`.

Usage of try...catch block is like below. It will handle runtime occurrence of IOException.

import java.io.*;
class Bufferedreaderclass{

public static void main(String[] arg)
{
    System.out.print("mazic of buffer reader \n Input : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = "";
    try{
        input = br.readLine();
        System.out.print("Your input is: "+input);
    } catch (IOException e) {
    // do something useful with this exception
    }
}

For more information on using try...catch, see https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html

Aakash
  • 2,029
  • 14
  • 22
  • i read documentation at https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html so you are saying "exception java.io.IOException;" is a "checked exception" by constructor of "BufferedReader" classs that should be resolve at compile time. Is it? – g1ji Jun 24 '15 at 10:02
  • Partly. `IOException` is a checked exception, because it extends `java.lang.Exception`, rather than `java.lang.RuntimeException`. `BufferedReader.readLine()` throws `IOException`, which should be caught using `try...catch` or re-thrown using `throws`. – Aakash Jun 24 '15 at 10:06
  • I think it is not compulsion to handle exception at compile time because theory say's "Exceptions are occurs at run time ". Then why exceptions in java.lang.Exception should be handle at compile time? – g1ji Jun 24 '15 at 10:23
  • You are not handling exception at compile time, you are just writing code to handle it during run-time. If you happen to get exception at run-time, this code will save your application to work without being terminated abruptly. It's like writing code to create object of a class at compile time, but actually the object will be created at run-time. – Aakash Jun 24 '15 at 10:57
  • ok i got it it's a just a way to anticipate such conditions and handle them properly so that your application doesn’t crash http://www.hacktrix.com/checked-and-unchecked-exceptions-in-java – g1ji Jun 24 '15 at 17:19
  • Exactly. Now you understood. – Aakash Jun 25 '15 at 02:46
1

The signature of readLine is this

public String readLine() throws IOException

Therefore you have to handle the exception that it might throw. Add try/catch or add throws IOException to your method.

In java there are checked exceptions and unchecked exceptions. This is a checked exception and you have to deal with it in one way or another. ArithmeticException is an example of an unchecked exception.

Simon
  • 6,293
  • 2
  • 28
  • 34
  • still it's not clear to me. please make me understand by explanation or any example @Simon – g1ji Jun 24 '15 at 09:31
  • @G4uKu3 It's a part of the java language. If a method says that it might throw an exception, then you have to deal with it or your code won't compile. The way of saying that a method might throw an exception is by adding `throws SomeException` after the method name. – Simon Jun 24 '15 at 09:43
  • I think it is not compulsion to handle exception at compile time because theory say's "Exceptions are occurs at run time ". – g1ji Jun 24 '15 at 09:52
  • Yes, exception never occur at compile time. But if the compiler sees that you don't handle checked exceptions then you get compiler errors. – Simon Jun 24 '15 at 11:53
  • I truly appreciate your time and effort to make me understand – g1ji Jun 24 '15 at 17:20