Interview Question

What is try-except block?

try runs risky code; except handles matching exceptions.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

A try-except statement handles exceptions raised while executing the try suite. • except clauses match exception classes. • Handlers should catch the narrowest exceptions they can address. • An optional else suite runs when no exception is raised.

💡 Simple Example

try: number = int('bad') except ValueError: print('Not a number')

Output

Not a number

⚡ Quick Revision

try runs risky code; except handles matching exceptions.