public class ExceptionExample {
public static void main(String args[]) {
try{}
//catch(SQLException e){} //Compile time Error
catch(Exception e){} // No Error
}
}
Asked
Active
Viewed 231 times
0

jmj
- 237,923
- 42
- 401
- 438

Jeetendra Sahu
- 99
- 1
- 1
- 4
-
`RuntimeException`s need not be mentioned in the method signature, therefore there's no way for the compiler to check, if an method call could throw a certain `RuntimeException` or not, but for non-`RuntimeException`s its different. But the answer to your question is simply: "Because the java specification says so." – fabian May 21 '15 at 18:48
2 Answers
1
because SQLException
is not a RuntimeException
, i.e. no code written in try block can throw it without declaring it,
where Exception
could be a RuntimeException

jmj
- 237,923
- 42
- 401
- 438
0
Checked exceptions must call a method the actually throws that exception, in the try clause.

Mordechai
- 15,437
- 2
- 41
- 82