Albo obsługujesz wyjątki albo deklarujesz w metodzie że taki wyjątek może zostać rzucony.
Przypadek z obsługą wyjątku IOException:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Program {
static public void main(String args[]) {
BufferedReader bufor=new BufferedReader(new InputStreamReader(System.in));
String zmienna=new String();
System.out.println("Wpisz tekst");
try {
zmienna = bufor.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("wczytalem tekst:"+zmienna);
}
}
Przypadek drugi - deklarujemy że metoda może wyrzucić wyjątek typu IOException:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Program {
static public void main(String args[]) throws IOException {
BufferedReader bufor = new BufferedReader(new InputStreamReader(System.in));
String zmienna = new String();
System.out.println("Wpisz tekst");
zmienna = bufor.readLine();
System.out.println("wczytalem tekst:"+zmienna);
}
}