User Tools

Site Tools


products:ict:python:exception_handling_questions

1. What is an exception in Python?

 Answer: An exception is an error that occurs during the execution of a program and disrupts its normal flow.

2. What is the purpose of exception handling?

 Answer: Exception handling allows you to catch and handle errors or exceptional conditions that may occur during the execution of a program.

3. How do you handle exceptions in Python?

 Answer: Exceptions can be handled using try-except blocks.

4. What is the syntax for a try-except block?

 Answer: The syntax is as follows:
         try:
             # Code that may raise an exception
         except ExceptionType:
             # Code to handle the exception

5. What is the role of the “finally” block in exception handling?

 Answer: The "finally" block is used to specify code that will be executed regardless of whether an exception was raised or not.

6. What is the purpose of the “else” block in exception handling?

 Answer: The "else" block is executed if no exceptions are raised in the corresponding try block.

7. How do you handle multiple exceptions in a single except block?

 Answer: You can use a tuple of exception types to handle multiple exceptions. For example:
         except (ExceptionType1, ExceptionType2):
             # Code to handle both exceptions

8. Can you have multiple except blocks for a single try block?

 Answer: Yes, you can have multiple except blocks to handle different types of exceptions.

9. What is the “raise” keyword used for?

 Answer: The "raise" keyword is used to manually raise an exception.

10. How do you create custom exceptions in Python?

  Answer: You can create custom exceptions by subclassing the built-in "Exception" class.

11. What is the purpose of the “assert” statement?

  Answer: The "assert" statement is used to assert that a certain condition is true. If the condition is false, an AssertionError is raised.

12. What is the difference between a built-in exception and a custom exception?

  Answer: Built-in exceptions are provided by Python and are used to handle common types of errors. Custom exceptions are created by users to handle specific situations in their programs.

13. How can you retrieve the error message associated with an exception?

  Answer: You can use the "str()" function or the "__str__()" method of the exception object to retrieve the error message.

14. What is the purpose of the “try-finally” block?

  Answer: The "try-finally" block is used to specify code that must be executed regardless of whether an exception occurred or not. The code in the "finally" block is guaranteed to run.

15. Can you have nested try-except blocks?

  Answer: Yes, you can have nested try-except blocks to handle exceptions in a hierarchical manner.

16. What happens if an exception is not caught?

  Answer: If an exception is not caught, it propagates up the call stack until it is caught by an outer try-except block or until it reaches the top level of the program, resulting in the program's termination.

17. What is the purpose of the “try-except-else” block?

  Answer: The "try-except-else" block is used when you want to specify code that should be executed if no exceptions are raised.

18. Can you have multiple “finally” blocks in a single try-except block?

  Answer: No, you can have only one "finally" block for a single try-except block.
products/ict/python/exception_handling_questions.txt · Last modified: 2023/05/12 12:28 by wikiadmin