1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: |
import java.io.*;
class Rei5_3{
public static void main(String[] args) throws IOException{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.print("1つめの値を入力:");
String str1 = br.readLine();
System.out.print("2つめの値を入力:");
String str2 = br.readLine();
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
num1 = num1 + num2;
System.out.println("2つの値を足すと:" + num1 + "になるですね!");
}
}
|
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: |
import java.io.*;
class Rei9_1{
public static void main(String[] args) throws IOException{
try{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.print("1つめの値を入力:");
String str1 = br.readLine();
System.out.print("2つめの値を入力:");
String str2 = br.readLine();
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
num1 = num1 + num2;
System.out.println("2つの値を足すと:" + num1 + "になるですね!");
}
catch(NumberFormatException nfe){
System.out.println("整数を入力してください。");
}
finally{
System.out.println("プログラムは終了されました♪");
}
}
}
|
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: |
import java.io.*;
class Rei9_2{
public static void main(String[] args){
try{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.print("1つめの値を入力:");
String str1 = br.readLine();
System.out.print("2つめの値を入力:");
String str2 = br.readLine();
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
num1 = num1 + num2;
System.out.println("2つの値を足すと:" + num1 + "になるですね!");
}
catch(NumberFormatException nfe){
System.out.println("整数を入力してください。");
}
catch(IOException ioe){
System.out.println("入出力エラー。");
}
finally{
System.out.println("プログラムは終了されました♪");
}
}
}
|
| 例外クラス名 | 説明 |
|---|---|
| ArrayIndexOutOfBoundsException | 配列の要素数を超えた値を入れたとき発生 |
| NumberFormatException | 正しく値が正しく変換されないとき発生 |
| IOException | 入出力エラーが発生したとき発生 |
| NullPointerException | 何も割り当てられていないオブジェクトを指したとき発生 |
| ArithmeticException | 0で割ってしまったとき発生 |
| Exception | どんな例外でもいいから発生したとき発生 |