University Of The People CS 1103: Programming 2 Dr. Salah Jabareen 14/04/2024.
Exceptions
Code expectations occur when something happens to the program code that disturbs its flow. That event that disturbs the code flow could either be considered an error or in other cases a code block that could be used to cut the flow of previous statements. Usually, when an error occurs in a code the expected outcome is for the code to crash, the program stops running and an error message is displayed (Eck, 2022). Here is when “code expectations” play a big part in your code as they identify the occurrence of potential errors beforehand in order to avoid program failure and allow your code to continue running. Implanting exceptions in your code will help prevent your …show more content…
Using a try-and-catch statement in the code basically prepares the code interpreter for potential errors, as it provides specific instruction to the interpreter on how to handle the occurrence of some specified errors by referring to the try-and-catch statement to fix the …show more content…
The user will start providing values like for example 5, 78.24, 156 and so on, if the user provides an invalid value like -87, -9, or -12.5 any of those values will cause the program to stop working and crash. That is where I could implement the try and catch statement so that instead of the program crashing a message will appear to the user that “ that value -12,5 is invalid, please provide another valid positive number”. That way, once the user corrects the input, the program will continue running.