Explicit Exception problem with try function

2024/11/23 20:39:23

I have to run codes irrespective whether it fails or not. I'm using ExplicitException. Following is my code:

try:G.add_edge(data1[0][0],data1[1][0],weight=data1[2+i][0])
except ExplicitException:passtry:G.add_edge(data1[0][0],data1[5][0],weight=data1[6+i][0])
except ExplicitException:passtry:G.add_edge(data1[0][0],data1[9][0],weight=data1[10+i][0])
except ExplicitException:pass   try:G.add_edge(data1[0][0],data1[13][0],weight=data1[14+i][0])
except ExplicitException:pass   

I'm getting the following error:

NameError: name 'ExplicitException' is not defined

would appreciate some help

Answer

I guess you got that idea from this answer. The idea the answer was trying to convey was that you can use an exception of your choice. In reality, there is no such exception as ExplicitException. You can use any exception from the built-ins or define your own exception class.

You can also except the base class Exception and except all exceptions.

try:# code
except Exception:pass

EDIT: While you can go about adding multiple try-except blocks, it is not a good practice. In your case, I believe your exception is because of some invalid value of i which would throw out of bounds exception. So you can avoid this by checking for the right values of i in if-else conditions.

If you're really into using try-except, try generalizing the lines and consolidating them into a loop. That would make the error handling easier. For example in the case above:

for j in range(1,14,4):try:G.add_edge(data1[0][0],data1[j][0],weight=data1[1+j+i][0])except:pass
https://en.xdnf.cn/q/120614.html

Related Q&A

How do I perform a bubble sort in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

check if number is between row of numpy array

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ? aa= np.array([[348 ,345],[460 , 459 ]])value = 347prin…

Print a word diagonally? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Click a button using Selenium and Python

I have the following code: <a class="sectionname" href="#" onclick="expandAll();return false;">Expand all</a> When I click on expand all, the whole page loads…

how to do Json format [duplicate]

This question already has answers here:How to store ner result in json/ database(2 answers)Closed 8 years ago.(S(PERSON Rami/NNP Eid/NNP)is/VBZstudying/VBGat/IN(ORGANIZATION Stony/NNP Brook/NNP Univers…

python numpy arange dtpye? why converting to integer was zero

x = np.arange(0.3, 12.5, 0.6)print(x)[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]x = np.arange(0.3, 12.5, 0.6,int)print…

Where is my syntax error?

Im trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:(reading_threshold =int(request.POST[reading_threshold]))I do…

programming challenge: how does this algorithm (tied to Number Theory) work?

In order to work on my python skills, I am sometimes doing various challenges on the internet (eg on hackerrank). Googling for something else, I found this problem, and the accompanying solution on the…

Running total for list of dict

Have a python list of dict as following:Dict1 = [{date: 1, name: xyz, qty: 100},{date: 1, name: xyz, qty: 200},{date: 1, name: xyz, qty: 300},{date: 1, name: xyz2, qty: 30},{date: 2, name: xyz, qty: 10…

How to convert CSV file to a specific JSON format with nested objects?

I want to populate my json message with data from a CSV file. I want each row to be a "new" json object. I am using Python and will connect the the code to an API once done. Some of the data …