TK Study

Software Engineering › Key terms › Code patterns › Python try / except

Python try / except

Code pattern Code patterns

Definition

try: risky code except ValueError: handle the error; keeps the program running with a helpful message.

Example

except ValueError: print("Numbers only")

Found in: syllabus, school notes.

Where it is used in the test

Syllabus area: Code patterns.

Question types it can appear in

Where it has appeared in the paper

Secure Software Architecture (Part B). Part letters follow each paper’s own layout.

Related past questions

These are the questions the flashcard set links to this term. Open each one and compare your answer with its marking criteria.

Study links

How to use it in an answer

A model sentence

General advice, built from the definition and example above

Code pattern: “try: risky code except ValueError: handle the error; keeps the program running with a helpful message.” Then apply it: except ValueError: print("Numbers only")

In a short answer, give the definition in your own words first, then tie it to the situation in the question. Do not copy the example above unless it fits the scenario.

Technique for the question types

What the marking guidelines and feedback say

Sample exam Q18b (3 marks): Developing secure code (Python input validation)

Remember to call the function in the main program after defining it, e.g. print(validate_username(input('Username: '))).

Worked answers from the marking guidelines

Sample exam Q18b (3 marks): Developing secure code (Python input validation) (3 marks): criteria and sample answer

Criteria

3: Correct function in Python to check whether a username satisfies the rules. 2: Partially correct function. 1: Some understanding of the problem and Python.

Sample answer

def validate_username(username): valid = True

 # length check
 if len(username) < 8:
      valid = False
 elif (not username[0].isupper()):
      valid = False
 elif (not username[-2].islower()):
      valid = False
 elif (not username[-1].isdigit()):
      valid = False

 return valid

Open the question page

Common mistakes

Marker feedback on other questions of the same type (code and diagram feedback often applies across questions)
  • Python questions (2025 HSC exam Q19): process string inputs; use a selection structure; code must execute in the environment; use correct indentation; call functions in the main program after defining them.
  • Python questions (2025 HSC exam Q26): replicate the grid; use the random library; use selection and looping to access the list; include a method of checking the number of values; use meaningful variable names and constants; avoid overly complex structures such as nested IF loops.
General advice
  • Use the term exactly as the syllabus words it, then link it to the scenario in the question; the 2025 feedback repeatedly rewards using the stimulus.
  • Answer the command word: outline gives main points only, explain gives a reason or cause, discuss gives both sides.

Same group in the syllabus: Python input and output · Python selection (if elif else) · Python for loop and range · Python while loop · Python strings and slicing · Python lists · Python 2D list (grid) · Python random numbers · Python functions · Global and local variables · Python file handling · Python modules and libraries · Python classes and objects · Python validation function pattern