我的代碼
networkService.setOnFailed(event -> {
Throwable exception = event.getSource().getException();
System.out.println(exception);
}
)
很難驗證我的例外是某種型別。當我列印它時,我得到了這個:
myCustomException: File C:\Users\...\Documents\...\test\...zip does not exist or is not a regular file我只想在它確實是而不是另一個例外的情況myCustomException下才去做。myCustomException
我想我可以使用正則運算式只從這個字串中得到這個,但我認為如果有意義的話,可能有一種方法可以“更干凈”地做到這一點。特別是如果有另一個例外可能對我自制的正則運算式決議器反應不佳。
uj5u.com熱心網友回復:
不,不是。這是一個Throwable- 你的代碼說它,就在那里:Throwable exception。
該println方法有一些變體;每個基元一個(例如double和int);顯然在這里不相關。然后一個 for String,那部分是顯而易見的。最后,一個對任何Object- 它將做的是呼叫toString()您傳遞給它的物件的方法,然后列印它。
這就是這里發生的事情(因為 Throwables 也是物件) - 您正在看到 toString 輸出。你不應該使用toString 輸出作為有意義的,它是一個除錯幫助,你不應該決議它。
以下是您可以做的幾件事:
if (exception instanceof MyCustomException) {
MyCustomException customEx = (MyCustomException) exception;
customEx.getCustomStuff();
}
exception.getClass() == MyCustomException.class // would be true
exception.getMessage();
等等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458700.html
上一篇:System.InvalidCastException:無法在MySqlConnector.Core.Row.GetInt32(Int32序數)將VarChar轉換為Int32
