in this case how can i able to know what specific exception to catch instead of using Exception as general?
how do i know what exception to catch?
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Szymon
22 April 2021, 11:56
The input obtained from the console is always a String, but in this example we need it to be a number. As you probably already know - we need to parse the variable from String to a numeric value (supposedly an int). In order to do this we ought to use a Integer.parse() method. Find out what exception this method can throw and you'll know the answer. Using IntelliJ you can do this by pressing CNTR+Q while your cursor is on the method name. You can also read documentation on oracle.com
+1
John
22 April 2021, 13:59
i dont have pc or laptop cause i cant afford one im just a student, but ill find out what that method throws
0
Gellert Varga
22 April 2021, 18:52
Use this Java online IDE compiler! I think you can use it on a smartphone, too. With this you will be able to try and check all of your programs and you will be able to watch the outputs always. You can learn a lot if you do a lot of checks and code-experiments.
I love this compiler very much:
https://www.jdoodle.com/online-java-compiler/
0
Gellert Varga
22 April 2021, 19:08
Write a test program.
Put an Integer.parseInt ("abc") command into a try-catch structure.
(This argument is a String that cannot be converted to an int, so it will throw an Exception.)
After this write this code in the catch block:
and the output will tell you exactly the answer to what kind of Exception this is. 0