What does except Exception as e mean in python? [closed]

2024/10/7 4:34:50

The typical structure for exception handling is below:

try:pass
except Exception, e:raise
else:pass
finally:pass

May I know what does except Exception, e:orexcept Exception as e: mean? Typically I will use print (e) to print the error message but I am wondering what the program has done to generate the e.

If I were to construct it in another way (below), how would it be like?

except Exception:e = Exception.something

What should the method be to replace the something?

When the body of code under try gives no exception, the program will execute the code under else. But, what does finally do here?

Answer

except Exception as e, or except Exception, e (Python 2.x only) means that it catches exceptions of type Exception, and in the except: block, the exception that was raised (the actual object, not the exception class) is bound to the variable e.

As for finally, it's a block that always gets executed, regardless of what happens, after the except block (if an exception is raised) but always before anything else that would jump out of the scope is triggered (e.g. return, continue or raise).

https://en.xdnf.cn/q/70280.html

Related Q&A

Numpy efficient big matrix multiplication

To store big matrix on disk I use numpy.memmap.Here is a sample code to test big matrix multiplication:import numpy as np import timerows= 10000 # it can be large for example 1kk cols= 1000#create some…

Basic parallel python program freezes on Windows

This is the basic Python example from https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool on parallel processingfrom multiprocessing import Pooldef f(x):return x*xif __na…

Formatting multiple worksheets using xlsxwriter

How to copy the same formatting to different sheets of the same Excel file using the xlsxwriter library in Python?The code I tried is: import xlsxwriterimport pandas as pd import numpy as npfrom xlsxw…

Good python library for generating audio files? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Keras ConvLSTM2D: ValueError on output layer

I am trying to train a 2D convolutional LSTM to make categorical predictions based on video data. However, my output layer seems to be running into a problem:"ValueError: Error when checking targe…

debugging argpars in python

May I know what is the best practice to debug an argpars function.Say I have a py file test_file.py with the following lines# Script start import argparse import os parser = argparse.ArgumentParser() p…

Non-blocking server in Twisted

I am building an application that needs to run a TCP server on a thread other than the main. When trying to run the following code:reactor.listenTCP(ServerConfiguration.tcpport, TcpCommandFactory()) re…

Reading changing file in Python 3 and Python 2

I was trying to read a changing file in Python, where a script can process newly appended lines. I have the script below which prints out the lines in a file and does not terminate.with open(tmp.txt,r)…

How to remove timestamps from celery pprint output?

When running the celery worker then each line of the output of the pprint is always prefixed by the timestamp and also is being stripped. This makes it quite unreadable:[2015-11-05 16:01:12,122: WARNIN…

How to get max() to return variable names instead of values in Python?

So I would like to get the maximum value from 3 variables, x,y,z.x = 1 y = 2 z = 3 max(x, y, z) # returns 3 but I want "z"However this returns the value of z i.e 3. How do I get the name of …