A runtime error is a problem that occurs while a program is running, causing it to stop or behave unexpectedly. It happens after the program has successfully compiled.
Click to reveal answer
beginner
Why is runtime error handling important?
Runtime error handling helps programs detect and manage errors during execution, preventing crashes and allowing the program to respond gracefully or recover.
Click to reveal answer
beginner
Name two common types of runtime errors.
Two common runtime errors are division by zero and accessing invalid memory (like null pointer errors).
Click to reveal answer
intermediate
How does a runtime error handler work in a program?
A runtime error handler detects errors during program execution, then takes actions like showing error messages, stopping the program safely, or trying to fix the problem.
Click to reveal answer
beginner
What is the difference between compile-time errors and runtime errors?
Compile-time errors are found by the compiler before running the program, like syntax mistakes. Runtime errors happen while the program runs, like dividing by zero.
Click to reveal answer
When do runtime errors occur?
AWhile the program is running
BDuring compilation
CBefore writing code
DAfter program termination
✗ Incorrect
Runtime errors happen during the execution of the program, not before or after.
Which of the following is an example of a runtime error?
ADivision by zero
BSyntax error
CMissing semicolon
DIncorrect variable declaration
✗ Incorrect
Division by zero is a runtime error because it happens when the program runs.
What is the main goal of runtime error handling?
ATo detect errors before running the program
BTo fix errors in the source code
CTo manage errors during program execution
DTo speed up compilation
✗ Incorrect
Runtime error handling manages errors that occur while the program is running.
Which of these is NOT a runtime error?
AArray index out of bounds
BSyntax error
CNull pointer access
DStack overflow
✗ Incorrect
Syntax errors are compile-time errors, not runtime errors.
How can a program respond to a runtime error?
AIgnore it and continue silently
BChange the source code automatically
CRestart the computer
DShow an error message and stop safely
✗ Incorrect
Programs usually show an error message and stop safely or try to recover when a runtime error occurs.
Explain what runtime error handling is and why it is necessary in programs.
Think about what happens when a program encounters a problem while running.
You got /3 concepts.
Describe the difference between compile-time errors and runtime errors with examples.
Consider when the error is found: before running or during running the program.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of runtime error handling in a program?
easy
A. To design the program's user interface
B. To manage unexpected problems that occur while the program is running
C. To improve the program's loading speed
D. To write comments in the code
Solution
Step 1: Understand runtime errors
Runtime errors happen when a program runs and encounters unexpected problems like dividing by zero or missing files.
Step 2: Purpose of handling these errors
Handling runtime errors helps the program respond safely without crashing, improving user experience.
Final Answer:
To manage unexpected problems that occur while the program is running -> Option B
Hint: Try-catch blocks catch errors; others are loops or conditions [OK]
Common Mistakes:
Confusing if or loops with error handling
Missing catch block after try
Using wrong keywords for error handling
3. Consider this pseudocode: try { result = 10 / 0; print(result); } catch (error) { print("Error caught"); } finally { print("Done"); } What will be the output when this code runs?
medium
A. Error caught Done
B. 10 Done
C. Done Error caught
D. Runtime error and no output
Solution
Step 1: Analyze try block
Dividing 10 by 0 causes a runtime error, so the print(result) is skipped.
Step 2: Catch and finally blocks
The catch block prints "Error caught". The finally block always runs and prints "Done".
Final Answer:
Error caught Done -> Option A
Quick Check:
Error caught then finally runs = Error caught Done [OK]
Hint: Error triggers catch, finally always prints last [OK]
Common Mistakes:
Expecting result to print despite error
Ignoring finally block output
Thinking error stops all output
4. Identify the error in this pseudocode for runtime error handling: try { print("Start"); } catch { print("Error"); }
medium
A. Catch block must come before try block
B. Missing finally block
C. Missing error parameter in catch block
D. Try block cannot have print statements
Solution
Step 1: Check catch block syntax
Most languages require catch to specify an error parameter to receive the error object.
Step 2: Validate other parts
Try block can have print statements, finally is optional, and catch must follow try.
Final Answer:
Missing error parameter in catch block -> Option C
Hint: Catch must name error to handle it properly [OK]
Common Mistakes:
Thinking finally block is mandatory
Believing print can't be in try
Reordering try and catch blocks
5. You want to write a program that reads a list of numbers and divides 100 by each number. If a division by zero occurs, it should skip that number and continue. Which runtime error handling approach is best?
hard
A. Use try-catch inside the loop to catch division errors and continue
B. Use a single try-catch around the whole loop and stop on error
C. Do not use try-catch; just check if number is zero before dividing
D. Use finally block to skip zero values
Solution
Step 1: Understand the problem
We want to handle errors for each division separately and continue processing all numbers.
Step 2: Choose error handling approach
Using try-catch inside the loop catches errors per division and allows continuing. A single try-catch stops on first error. Checking zero before dividing is good but not runtime error handling. Finally block is for cleanup, not skipping values.
Final Answer:
Use try-catch inside the loop to catch division errors and continue -> Option A
Quick Check:
Catch errors per item to continue = Use try-catch inside the loop to catch division errors and continue [OK]
Hint: Catch errors inside loop to handle each case separately [OK]
Common Mistakes:
Stopping loop on first error
Not using try-catch for runtime errors
Misusing finally block for logic control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.