Guys when i omit | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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"); } }

28th Jun 2020, 6:42 AM
stephen haokip
stephen haokip - avatar
3 Answers
+ 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.
28th Jun 2020, 7:18 AM
ChaoticDawg
ChaoticDawg - avatar
0
stephen haokip , I tried it in Playground and it works. I don't know what do you mean?
28th Jun 2020, 7:07 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWhiteCat If u take out throws Exception it wont
28th Jun 2020, 7:11 AM
stephen haokip
stephen haokip - avatar