Exception handling is the basis of java, but since it was misrecognized, it is left as a memorandum.
I wanted to throw an exception to the method caller, so when I tried to throw the caught exception, I got a compile error for some reason.
For example, when throwing a timeout error, it is as follows.
public void foo(){
try {
fetch();
} catch (SocketTimeoutException e) {
}
}
public Integer fetch(){
try {
//Http通信
} catch (SocketTimeoutException e) {
throw e; // compile error here
}finally {
}
return number;
}
Besides throw There is a thorws clause, but I thought it had the same function as the catch statement. But in fact the method with it meant ** a method that could throw an exception **.
So I added a throws clause to the method throwing the exception and the compile error disappeared and I could catch it with foo ().
I didn't think I would stumble on the foundation of the foundation, but I'm glad I knew it now ...
Recommended Posts