Skip to content

Instantly share code, notes, and snippets.

@takanakahiko
Created January 7, 2015 07:55
Show Gist options
  • Save takanakahiko/3edfa01cce6c5d1146e5 to your computer and use it in GitHub Desktop.
Save takanakahiko/3edfa01cce6c5d1146e5 to your computer and use it in GitHub Desktop.
例外クラスの作成の学習。これでも正しいのかさっぱり(1.7)
class EvenException extends Exception{};
class OddException extends Exception{};
class Even{
static void isEven(int i) throws EvenException, OddException{
if(i%2 == 0){
throw new EvenException();
}else{
throw new OddException();
}
}
public static void main(String[] args){
int n = Integer.parseInt(System.console().readLine("数字を入力してください ->"));
try{
isEven(n);
}catch(EvenException e){
System.out.println("偶数です");
}catch(OddException e){
System.out.println("奇数です");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment