CodeGym /Courses /New Java Syntax /Catch me if you can

Catch me if you can

New Java Syntax
Level 15 , Lesson 6
Available
Catch me if you can - 1

"Hi, Amigo. Here are some interesting tasks that involve catching exceptions."

15
Task
New Java Syntax, level 15, lesson 6
Locked
Exception when working with numbers
Catch the exception that occurs when the following code is executed: int a = 42 / 0; Display the type of exception caught.
15
Task
New Java Syntax, level 15, lesson 6
Locked
Exception while working with strings
Catch (and display the type of) the exception that occurs when the following code is executed: String s = null; String m = s.toLowerCase();
15
Task
New Java Syntax, level 15, lesson 6
Locked
Exception when working with arrays
Catch the exception that occurs when the following code is executed: array[8] = 5; Display the exception on the screen using any means (the output must contain the type of the exception thrown).
15
Task
New Java Syntax, level 15, lesson 6
Locked
Exception when working with List collections
Catch the exception that occurs when the following code is executed: String s = list.get(18); Display the exception on the screen using any means (the output must contain the type of the exception thrown).
15
Task
New Java Syntax, level 15, lesson 6
Locked
Exception when working with Map collections
Catch the exception that occurs when the following code is executed: HashMap map = new HashMap(null); Display the exception on the screen using any means (the output must contain the type of the exception thrown).
15
Task
New Java Syntax, level 15, lesson 6
Locked
Exception when working with numbers
Catch the exception that occurs when the following code is executed: int num = Integer.parseInt("XYZ"); Display the exception on the screen using any means (the output must contain the type of the exception thrown).

"Here's a tip: write your program, see what sort of exceptions occur, and then change the code to catch them."

Comments (46)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Justin Smith Level 41, Greenfield, USA, United States
21 July 2021
One thing I have noticed going through these tasks and lessons is that where it says "this task has been completed by n students"... n gets smaller the farther you get into the course. So remember that as you keep persevering through the lessons, you're putting yourself into a smaller and smaller group of people who are learning more about Java programming than those who quit. Keep it up!
Anonymous #10775689 Level 14, United Kingdom
2 August 2021
Nice Justin 👍. You're a few levels ahead of me now. Any tips for the coming levels?
Stanislav Mayer Level 15, Czech Republic
6 February 2022

if (Justin Smith.comment != true) {
System.out.println("Actually, this is not always true. Tasks in chapter 9-2 (easy ones) were completed by more people than tasks in chapter 8-11 (hard ones");
System.out.println("Justin, I think your statement needs to throw an exception!");
Justin Smith.comment.rating++; //I still understand the motivation factor in your statement :-)
}
DarthGizka Level 24, Wittenberg, Germany
30 May 2021
task0907 (Exception when working with numbers) caveat: you will get failed if you catch RuntimeException, even though the requirements prohibit only Exception. Do not let IntelliJ add warning suppression annotations to the arithmetic statement; this will result in failure of requirement #5 "Don't delete the existing code in the main method." (yes, the mind boggles).
Jonaskinny Level 25, Redondo Beach, United States
20 February 2022
One rule of thumb is that you should not catch RuntimeException because it is used for unpredictable exceptions, so if you know how to handle it in your catch block, you should fix the code instead of fixing it in the catch. And if you cant fix it in the catch, you should not catch it, so that it does not accidentally get silenced. So either way, one should likely not catch RuntimeException or any un-recoverable exception, unless you want to do some last minute bailout code, and then re-throw RuntimeException so it does not get silenced.
Russell Level 9, Galmpton, Brixham , United Kingdom
30 April 2021
Ahh back on form after the stack trace debacle. Nice one codegym good lessons to breed familiarity with the types of exceptions
Nathan Level 13, Below the firmament, United Kingdom
13 March 2021
Great lessons!
Andrei Level 41
11 November 2020
Very nice tasks, easy , approachable and good to reinforce what has just been studied! Thanks!
Chandan Thapa Level 22, Dubai, United Arab Emirates
30 October 2020
Didn't have to keep looking at my watch while finishing these tasks in this particular section! :)
orangepeeler Level 14, Not in list, United States
24 August 2020
https://programming.guide/java/list-of-java-exceptions.html
Lawson Level 29, Lagos, Nigeria
13 August 2020
I am surprised The tasks were easy .. So unexpected
Petros Level 23, Columbia, United States
27 July 2020
What a breath of fresh air that these exercises were.
Thang Za Thang Level 18, Melbourne, Australia
30 June 2020
For once in a long time, I did not want to blow my brains out...
matt Level 24, Springfield, United States
12 August 2020
Heck yea