Questions tagged [try-except]

This method of error management is commonly used in Python and Delphi, mirroring the try/catch mechanism found in C-based programming languages. Additionally, it is a feature specific to Microsoft's C and C++ extensions.

An error is triggered, resulting in a TypeError being raised

I have a question regarding try-except statements that is different from the other questions mentioned. I am not in need of a finally statement... Consider the following code snippet: try: #dosomething except TypeError: #dosomething -> THIS RE ...

Is it necessary to have separate try-except blocks for different KeyErrors that are closely related? Can these be combined in a single try block without sacrificing functionality?

Imagine a scenario where I need to print results from a request response. If I have multiple parameters that I want to check but may not always be present in every response, I can handle this by using a try clause to catch any KeyError. The issue is that ...

What is the best way to exit the while loop and proceed directly to the finally block when the user inputs the term "done"?

largest = None smallest = None numbers = [] while True: try: user_input = input("Please enter a number: ") except NameError as e: if e == "done": break else: print("Your input is inv ...