この例ではIO例外が発生しません。このコードでIOExceptionがスローされない理由
public static void main(String[] args){
double r = 0;
System.out.println("Please enter radius of a circle");
try{
Scanner sc = new Scanner(System.in);
r = sc.nextDouble();
}catch(NumberFormatException exe){
System.out.println("Inpvalid radius value");
}catch(IOException exe){
System.out.println("IO Error :" + exe);
}
double per = 2 * Math.PI *r;
System.out.println(per);
}
この以下のプログラムでは、エラーは表示されません。 のpublic static無効メイン(文字列[] args){
int radius = 0;
System.out.println("Please enter radius of a circle");
try
{
//get the radius from console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
//if invalid value was entered
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter of a circle is " + perimeter);
それが起こっている理由を私は理解していません。両方が同じ目的をやっているので、なぜ最初のコードは、なぜあなたはそれをキャッチしている、にIOExceptionが