Programming technique
Debugging and exceptions
Debugging uses evidence to locate and correct faults. Exception handling allows a program to respond safely to unexpected input or unavailable resources.
Three common error categories
| Error type | What happens | Example |
|---|---|---|
| Syntax error | The program cannot compile because the language rules were broken. | Missing semicolon or misspelled keyword. |
| Runtime error | The program starts but fails during execution. | Invalid number conversion or unavailable file. |
| Logic error | The program runs but produces the wrong result. | Incorrect formula or branch order. |
| Error type | What happens | Example |
|---|---|---|
| Syntax error | The interpreter cannot begin normal execution because the language rules were broken. | Missing colon, incorrect indentation or misspelled keyword. |
| Runtime error | The program starts but fails during execution. | Invalid number conversion or unavailable file. |
| Logic error | The program runs but produces the wrong result. | Incorrect formula or branch order. |
Catch a specific exception
Handle a specific exception
Catch the most specific exception you can handle meaningfully. A catch block should explain what the user can do next rather than merely printing a technical stack trace.
Handle the most specific exception you can respond to meaningfully. An except block should explain what the user can do next rather than merely printing a technical traceback.
Finally
A finally block runs whether or not an exception occurred. It is useful for cleanup or guaranteed final actions.
Debug systematically
Change one suspected cause at a time so the result of each test remains meaningful.
- Reproduce the problem with a specific test case.
- State the expected and actual result.
- Narrow the fault using a trace table, print statement, breakpoint or step execution.
- Change one suspected cause.
- Retest the original case and nearby boundary cases.
Validation is not the same as exception handling
Range, presence, length and format checks prevent invalid values; exceptions handle failures that still occur.
Do not hide logic errors
A broad exception handler cannot make an incorrect calculation correct.
Keep evidence
Record the test input, variable values and exact correction so the fault can be explained and reproduced.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Safe Integer Input
SelectedAsk the user for a whole number. Prevent the program from crashing when text or a decimal is entered. Keep asking until a valid integer is provided, then display the accepted value.
Reliable Division
SelectedAsk for two integers and divide the first by the second. Handle invalid numeric input and division by zero with specific messages. The program must not catch every possible exception as one vague error.
Broken Program Investigation
SelectedCollect the debugging evidence sheet from the front of the class. Create or obtain a short program containing at least one syntax error, one runtime error and one logic error. Use compiler messages, print tracing or breakpoints, and step-by-step execution to locate and correct each fault. Record the test case, evidence, cause and correction for every error, then hand the sheet to your teacher.
Expression Parser
SelectedAllow the user to enter a calculation as one string, such as 16+8, 120/5 or 9*13. Identify the operator, extract both operands and output the result. Support +, -, * and /. Detect malformed expressions, non-numeric operands and division by zero without crashing.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.