# Error Handling > Tenth graders write robust programs that do not crash on bad input. Risky code goes in a try block; if it fails, an except block catches the error and… **Computer Science** | **Grade 10** *A free lesson plan from K12 Academics. Source page: https://lessonplans.k12academics.com/k12-lesson-g10-cs-error-handling.html* Tenth graders write robust programs that do not crash on bad input. Risky code goes in a try block; if it fails, an except block catches the error and recovers gracefully. They sort what goes in try vs except, then add error handling to a program. **Copy link** works with Canvas, Schoology, Moodle and any other LMS — paste it in as a resource or assignment. ## Everything you need before the bell rings ### Learning Objectives Students will be able to… - Use **try / except**. - Put **risky code** in try. - **Recover** in except. - Make a program robust. How do you keep a program from crashing when something goes wrong? ## try Block or except Block? Project this and sort each item into the try block (risky code) or the except block (handle the error). ## 55 minutes, five moves Tap any phase to open the teacher moves and student actions. ### Hook — When It All Goes Wrong What happens if a user types “cat” where a number is expected? #### 👩‍🏫 Teacher Moves - Show a program crashing. - Ask how to prevent it. - Set the goal. #### 🎒 Student Actions - Look. - Guess. - Get ready. ### Use — Error Handling Students use the idea. #### 👩‍🏫 Teacher Moves - Send students to try or except? - Sort each item. - Note risky code vs recovery. #### 🎒 Student Actions - Sort. - Say. - Note it. ### Modify — Catch It Students modify. #### 👩‍🏫 Teacher Moves - Wrap risky code in a try. - Add an except to recover. - Test a bad input. #### 🎒 Student Actions - Wrap. - Add. - Test. ### Create — Make It Robust Students create. #### 👩‍🏫 Teacher Moves - Add try/except to a program. - Give a friendly message. - Use a safe default. #### 🎒 Student Actions - Add. - Message. - Default. ### Reflect — Reflect Students close. #### 👩‍🏫 Teacher Moves - Say what goes in try. - Say what except does. - Complete the exit ticket. #### 🎒 Student Actions - Say it. - Say. - Complete the exit ticket. ## Built to the standards you report on Aligned to the CSTA K-12 Computer Science Standards and the ISTE Standards for Students. Evaluate and refine artifacts for correctness and robustness. Decompose problems into components. Iteratively develop programs using control structures. Test for and handle edge cases. ## One lesson, every learner ### Multilingual Learners - Act out “try, then catch.” - Frame: “risky here, recover there.” - Pair talk first. ### Support & Access - Sort try vs except only. - Wrap one risky line. - Use a template. ### Stretch & Extend - Catch specific error types. - Add a finally block. - Validate input before trying. ### What to gather - 📽️Screen / board - 🛡️try/except cards - 💻try or except? - 🐍Python / editor - 📓Notebooks - 🎫Exit-ticket slips ### Key terms - **error / exception**: a problem that interrupts a program - **try**: block holding code that might fail - **except**: block that runs if the try fails - **catch**: to handle an error instead of crashing - **robust**: keeps working on bad input - **crash**: the program stops with an error - **default**: a safe fallback value - **validate**: check input before using it ## Exit Ticket Preview the three formative checks. Tap “Sample answer” to see what mastery looks like — hide them before you print for students. ### Reflect: error handling. Have students wrap one risky line in try/except with a friendly message. A printable error-handling worksheet is in the Computer Science library. ## Check your understanding **1. What does “error / exception” mean?** - a problem that interrupts a program *(correct)* - block holding code that might fail - the program stops with an error - check input before using it “error / exception” means: a problem that interrupts a program **2. Which word means: “block holding code that might fail”?** - try *(correct)* - robust - default - catch “try” means: block holding code that might fail **3. What does “except” mean?** - keeps working on bad input - block that runs if the try fails *(correct)* - a problem that interrupts a program - a safe fallback value “except” means: block that runs if the try fails **4. Which word means: “to handle an error instead of crashing”?** - crash - catch *(correct)* - default - except “catch” means: to handle an error instead of crashing **5. What does “robust” mean?** - keeps working on bad input *(correct)* - check input before using it - a problem that interrupts a program - a safe fallback value “robust” means: keeps working on bad input **6. Which word means: “the program stops with an error”?** - crash *(correct)* - catch - error / exception - except “crash” means: the program stops with an error ## Printable worksheet --- Full lesson page: https://lessonplans.k12academics.com/k12-lesson-g10-cs-error-handling.html All lessons: https://lessonplans.k12academics.com/