0
Guys when i omit
throws Exception The code won't compile Please tell me why import java.io.*; public class Solution{ public static void main(String[] args) throws Exception{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); System.out.println(s+" makes $12000 a year. Ha-ha-ha"); } }
3 ответов
+ 4
BufferedReader throws IOException, which is a checked exception. Checked exceptions are checked by the compiler at compile time and must be handled either by try catch or by throwing the exception up the callstack to a point at which it is handled. Since this is used in the main method and not handled via a try catch it is thrown up to the JVM where it is handled. Omitting either results in the error.
https://www.tutorialspoint.com/java/java_exceptions.htm#:~:text=An%20exception%20(or%20exceptional%20event,exceptions%20are%20to%20be%20handled.
0
stephen haokip , I tried it in Playground and it works. I don't know what do you mean?
0
TheWhiteCat
If u take out throws Exception it wont