在測試情況下,我也行未處理的異常 - 拋出不工作
ZahlPasstNicht zahlPasstNicht = new ZahlPasstNicht(min, max, ist);
第二部分是強調了紅色和它說:未處理的異常:ZahlPasstNicht
我可以使用嘗試/ catch或讓構造函數拋出一個錯誤,我試圖做後者:
public class ZahlPasstNicht extends IndexOutOfBoundsException {
int a;
int b;
int c;
public ZahlPasstNicht(int a, int b, int c) throws ZahlPasstNicht {
this.a = a;
this.b = b;
this.c = c;
}
我在做什麼錯?
這是一個奇怪的用例。拋出異常的可能性爲零,異常類不應拋出異常。 –
如果你在構造'ZahlPasstNicht'時設法導致'ZahlPasstNicht'類型的異常,我預見到一個無限循環......我曾經看到一個系統在記錄導致它記錄錯誤的錯誤時拋出一個錯誤。 。日誌文件爆炸到50Gb。 – Persixty
另外,拋出'throws RuntimeException'是一種糟糕的做法,其中'IndexOutOfBoundsException'是'RuntimeException'。未經檢查的異常不需要「拋出」。只有把'throws'用於檢查異常。 –