Java:独自のExceptionエラーメッセージを簡単に設定
860 ワード
// 1 , ,
public Node getNode(int ID) throws NoSuchNodeException{
for(Node oneNode:nodes)
if(oneNode.getId()==ID)
return oneNode;
throw new NoSuchNodeException("no Node with ID "+ID+"in this Graphs");
// ,
}
// 2 , try catch ,
// catch ,
try {
graph.getNode(42);
} catch (NoSuchNodeException e1) {
System.out.println("No such node: " + e1.getMessage());
}
// 3 , , ,getMessage
class NoSuchNodeException extends Exception {
private static final long serialVersionUID = 1L;
String message;
public NoSuchNodeException(String ErrorMessagr) {
message = ErrorMessagr;
}
public String getMessage() {
return message;
}
}