Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python try-else

What is the intended use of the optional else clause of the try statement?

like image 599
geowa4 Avatar asked May 13 '09 02:05

geowa4


People also ask

Is there a try catch in Python?

The try and except Block: Handling Exceptions. The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program's response to any exceptions in the preceding try clause ...

Can I use if else in TRY except Python?

Use the Python try... except...else statement provides you with a way to control the flow of the program in case of exceptions. The else clause executes if no exception occurs in the try clause. If so, the else clause executes after the try clause and before the finally clause.

Is try except Pythonic?

Try Except in PythonThe try block is used to check some code for errors i.e the code inside the try block will execute when there is no error in the program. Whereas the code inside the except block will execute whenever the program encounters some error in the preceding try block.

What is try finally in Python?

The finally keyword is used in try... except blocks. It defines a block of code to run when the try... except...else block is final. The finally block will be executed no matter if the try block raises an error or not.


2 Answers

The statements in the else block are executed if execution falls off the bottom of the try - if there was no exception. Honestly, I've never found a need.

However, Handling Exceptions notes:

The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try ... except statement.

So, if you have a method that could, for example, throw an IOError, and you want to catch exceptions it raises, but there's something else you want to do if the first operation succeeds, and you don't want to catch an IOError from that operation, you might write something like this:

try:     operation_that_can_throw_ioerror() except IOError:     handle_the_exception_somehow() else:     # we don't want to catch the IOError if it's raised     another_operation_that_can_throw_ioerror() finally:     something_we_always_need_to_do() 

If you just put another_operation_that_can_throw_ioerror() after operation_that_can_throw_ioerror, the except would catch the second call's errors. And if you put it after the whole try block, it'll always be run, and not until after the finally. The else lets you make sure

  1. the second operation's only run if there's no exception,
  2. it's run before the finally block, and
  3. any IOErrors it raises aren't caught here
like image 183
Blair Conrad Avatar answered Oct 11 '22 01:10

Blair Conrad


There is one big reason to use else - style and readability. It's generally a good idea to keep code that can cause exceptions near the code that deals with them. For example, compare these:

try:     from EasyDialogs import AskPassword     # 20 other lines     getpass = AskPassword except ImportError:     getpass = default_getpass 

and

try:     from EasyDialogs import AskPassword except ImportError:     getpass = default_getpass else:     # 20 other lines     getpass = AskPassword 

The second one is good when the except can't return early, or re-throw the exception. If possible, I would have written:

try:     from EasyDialogs import AskPassword except ImportError:     getpass = default_getpass     return False  # or throw Exception('something more descriptive')  # 20 other lines getpass = AskPassword 

Note: Answer copied from recently-posted duplicate here, hence all this "AskPassword" stuff.

like image 31
Izkata Avatar answered Oct 11 '22 03:10

Izkata