異なる方法で処理したい場合は、異なる例外をキャッチします。
try{
}catch(IOException e1){
//-- if io error--
}catch(FormatException e2){
//--if format error--
}catch(Exception e3){
//--any thing else --
}
ほとんどのJavaAPI例外には特別な整数がなく、タイプ、メッセージ、および原因があります。
ただし、独自のタイプの例外を作成することもできます。
public class MyIntegerException extends Exception{
private int num;
public int getInteger(){
return num;
}
public MyIntegerException(int n, String msg){
super(msg);
this.num = n;
}
}
投げる :
throw new MyIntegerException(1024,"This is a 1024 error");
キャッチ:
catch(MyIntegerException e){
int num = e.getInteger();
//--do something with integer--
}