我正在爲星期五的CS考試而學習,並且在這裏的路上碰到一個碰撞。這個問題讓我去處理這個異常,然後用兩種不同的方法來傳播這個異常,但我覺得他們是一樣的。誰能幫忙?練習題如下所示。繁殖與處理有什麼區別?
您將得到下面的類:
public class ReadData {
public void getInput() {
getString();
getInt();
}
public void getString() throws StringInputException {
throw new StringInputException();
}
public void getInt() throws IntInputException {
throw new IntInputException();
}
}
class StringInputException extends Exception {}
class IntInputException extends Exception {}
上面的代碼將導致getInput編譯錯誤()方法。 使用兩種不同的技術重寫getInput()方法:
Method 1 - Handle the exception
Method 2 - Propagate the exception
使得代碼編譯。