Loading samples...
Loading samples...
py-sample-13
Try-except, finally, custom exceptions, and best practices
Which keyword catches exceptions?
Which executes always, exception or not?
Which block runs when no exception?
How to raise an exception?
What does except Exception as e: do?
Which catches division by zero?
How to create custom exception?
What does assert do?
Which is base class for all exceptions?
What happens if exception not caught?
Explain try-except-else-finally block execution flow.
[3 marks]What is the difference between except: and except Exception:?
[3 marks]Explain exception hierarchy in Python with diagram.
[3 marks]How to raise an exception with custom message? Give example.
[3 marks]Explain exception chaining with 'raise from'.
[3 marks]What are EAFP and LBYL programming styles? Which does Python prefer?
[3 marks]Explain traceback module and how to print full traceback.
[3 marks]When should you NOT catch an exception? Best practices.
[3 marks]What is contextlib and @contextmanager for exception handling?
[3 marks]Explain sys.exc_info() and its use cases.
[3 marks]Write a function that safely divides two numbers with proper exception handling.
[5 marks]Create a context manager for timing code execution using contextlib.
[5 marks]Write a program that reads file and handles all possible file-related exceptions.
[5 marks]Create a retry decorator that retries function up to 3 times on exception.
[5 marks]Write a custom ValidationError exception class with additional attributes.
[5 marks]Create a function that converts string to int with fallback default value on error.
[5 marks]Predict behavior: try: x = 1 / 0 except: print('Error') else: print('Success') finally: print('Done')
[3 marks]Explanation:Division by zero raises exception, caught by except. else skips. finally always executes.
Find error: try: f = open('file.txt') except FileNotFoundError: print('Missing') finally: f.close()
[3 marks]Explanation:If file doesn't exist, f is never assigned. finally tries to close undefined f. Use with statement or check.
Predict output: def func(): try: return 1 finally: return 2 print(func())
[3 marks]Explanation:finally always executes and its return overrides try's return. Returns 2, not 1.
Find issue: except Exception as e: print('Error:', e) raise e
[2 marks]Explanation:raise e creates new exception point. Use bare 'raise' to preserve original traceback.
Predict behavior: for i in [1, 'a', 2]: try: print(int(i)) except ValueError: continue print('OK')
[3 marks]Explanation:For 'a', ValueError caught, continue skips 'OK'. Output: 1, OK, 2, OK on separate lines.
Find error: try: risky() except: pass
[2 marks]Explanation:Bare except catches KeyboardInterrupt, SystemExit. Use except Exception: or be more specific.
Predict output: try: raise ValueError('bad value') except ValueError as e: print(e)
[2 marks]Explanation:Exception caught, message printed. String representation of exception is the message passed to constructor.
Find error: class MyError: pass raise MyError('error')
[2 marks]Explanation:Custom exceptions must inherit from Exception (or BaseException). Fix: class MyError(Exception):