🚨 Exceptions & RegEx

Exceptions & RegEx: Try-except is like a bike helmet — you might fall, but with the helmet nothing serious happens; the except block catches the code's "fall." But the real skill

Try-except is like a bike helmet — you might fall, but with the helmet nothing serious happens; the except block catches the code's "fall." But the real skill is knowing WHICH helmet to wear: write "except TypeError" when the actual error raised is ValueError, and the except block never ACTIVATES at all — wrong helmet, the fall still hurts. Running int("abc") raises ValueError, not TypeError — not knowing that distinction is exactly why people spend hours debugging "why isn't my except block catching this?" Java's catch (SpecificException e) demands the same discipline; a lazy catch-all habit (bare except: or catch (Exception e)) HIDES the real error — and in QA, a silently swallowed bug is far more dangerous than a visible one.

Java uses try-catch-finally. Python has the same structure: try-except-finally. Both inherit from a base Exception class. Difference: Python uses the 'as' keyword to access the exception object.

Micro Lab: Python coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: Python coding practice

Read inputs and data types

Complete the critical line with the smallest change

Run the code and compare expected output

If it fails, read traceback or assertion message

Make the result reusable for a test report

What is the safe order for running Python QA code?

Java vs Python — Error Handling

Which block in Python runs only when no exception occurs?

The 'else' block runs only when the try block completes without raising an exception. Java has no equivalent — this is unique to Python.