Skip to content

Instantly share code, notes, and snippets.

@typosone
Created July 2, 2014 02:48
Show Gist options
  • Save typosone/830ba372d6a9f9468d21 to your computer and use it in GitHub Desktop.
Save typosone/830ba372d6a9f9468d21 to your computer and use it in GitHub Desktop.
Java講義 サンプル (例外)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Cat {
public static void main(String...args) {
for (int i = 0; i < args.length; i++) {
System.out.println("ファイル名: " + args[i] + "====");
try {
BufferedReader reader =
new BufferedReader(new FileReader(args[i]));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("ファイルが見つかりません: " + e);
} catch (IOException e) {
System.out.println("I/Oエラーです: " + e);
}
}
}
}
public class ExceptionTest1 {
public static void main(String...args) {
int[] array = new int[3];
try {
System.out.println("代入します");
//array[100] = 0;
array[0] = 0;
System.out.println("代入しました");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("代入できませんでした");
System.out.println("例外は" + e + "です");
}
System.out.println("終了します");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment