What are the two main types of errors in Python?
Syntax errors and exceptions.
What is a syntax error in Python?
A parsing error indicating that code does not conform to Python’s grammatical rules.
How does Python indicate the location of a syntax error?
Python outputs the offending line and character position, along with the file name and line number if applicable.
What is an exception in Python?
An error detected during execution that does not necessarily stop the program and can be handled.
What happens if an exception is not handled in Python?
The program displays an error message and typically halts execution.
What information does the last line of a Python exception message provide?
It specifies the type of exception that occurred, such as ZeroDivisionError, NameError, or TypeError.
Are standard exception names special keywords in Python?
No, they are built-in identifiers but not reserved keywords.
What additional details does a Python exception message provide beyond the type?
It contains specific details based on the exception type and the cause.
How does Python display the context of an exception?
Using a stack traceback that shows the sequence of code that led to the error.
What section of Python documentation lists built-in exceptions and their meanings?
The Built-in Exceptions section.
How can a Python program handle specific exceptions?
By using a try statement with except clauses for selected exceptions.
What operations occur within a try statement in Python?
The try block runs; if no exception occurs, except is skipped, else it is executed if present.
What happens if an exception occurs within the try block and matches an except clause?
The matching handler runs, and execution resumes after the try/except block.
What does Python do if an exception is raised in the try block but does not match any except clause?
It passes the exception to outer try statements or stops execution with an error if unhandled.
Can a try statement in Python have multiple except clauses?
Yes, to handle different exception types.
How does Python determine which except handler to execute when multiple match?
Only the first matching handler is executed.
What exceptions does a class named in an except clause match?
Exceptions that are instances of that class or its subclasses, but not base classes.
How can a single except clause handle multiple exceptions in Python?
By specifying a parenthesized tuple of exception types.
Can exception handlers capture exceptions that arise in called functions within the try block?
Yes, handlers process exceptions from functions called from the try block.
How can a variable be assigned to a caught exception in except?
By specifying a variable after the exception type in the except statement.
What attribute usually holds the arguments for an exception instance?
The .args attribute.
How does the __str__() method of an exception relate to the error message?
Its output is shown as the final part of an unhandled exception message.
What class is the base for all exceptions in Python?
BaseException.
What is the base class for non-fatal exceptions
and which exceptions are usually not handled?