• Register
1 vote
221 views

Problem :

I am very new to Python programming. My below code gives me errors :

with open(json_file) as jfile:
    try:
        json_config = json.load(jfile)
    except ValueError as e:
        raise Exception('Invalid json: {}'.format(e))

Please find below errors caused by above code.

json.decoder.JSONDecodeError: Expecting ',' delimiter: line 103 column 9 (char 1093)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
<....>
raise Exception('Invalid json: {}'.format(e))
Exception: Invalid json: Expecting ',' delimiter: line 103 column 9 (char 1093)
Can some expert tell me how can I quickly fix above errors?
8 5 2
3,230 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

I think now you are having very strange issue which is related to raising a ValueError exception inside the another caught exception. If you are looking for the quick fix for your error then you need to change below line of code
raise Exception('Invalid json: {}'.format(e))
To the below correct one.
raise Exception('Invalid json: {}'.format(e)) from None

It will make your end code as below:

with open(json_file) as jfile:
    try:
        json_config = json.load(jfile)
    except ValueError as e:
        raise Exception('Invalid json: {}'.format(e)) from None

After making above changes in your code you must get a desired result of catching the exception. Hope the solution helped you in quick fix.

8 4 1
5,680 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: I recently came to know about the non-primitive data types in Java. I was trying to see what happens if I try to assign null to the instances of all non-primitive data types and to my surprise, all threw an exception except for the String ... 14. I am unable to understand why the compiler does not throw any exceptions with the string instance. Could someone please answer my question?
asked Apr 24 Code Learner 9.8k points
0 votes
1 answer 12 views
12 views
Problem: What are the effects of exceptions on performance in Java?
asked Mar 9 ummesalma 29.2k points
0 votes
1 answer 5 views
5 views
Problem: I am trying to learn programming on my own time and I am still trying to get the hang of it. I am getting the following error: java.io.IOException: The handle is invalid
asked Apr 19 ummesalma 29.2k points
0 votes
1 answer 2 views
2 views
Problem: Which will be thrown when handling insufficient memory?
asked Apr 16 sumaiya simi 43.9k points
1 vote
1 answer 3 views
3 views
Problem : Do you have any idea how to fix Exception _mysql_exceptions.OperationalError > (2013, 'Lost connection to MySQL server during query')
asked Apr 26 Abik Dey 4k points
1 vote
1 answer 6 views
6 views
Problem: Need help with the following issue. An unhandled exception of type 'system.io.filenotfoundexception' occurred in mscorlib.dll
asked Apr 23 PkGuy 23.5k points
0 votes
1 answer 29 views
0 votes
1 answer 2 views
2 views
Problem: I am stuck now .. need some suggestions .. please help me .. C++ exception handling best practices
asked Apr 27 Ifra 34.7k points
0 votes
1 answer 6 views
6 views
Problem: I'm working on exception handling and right now I'm stuck on a problem I'm working on out of my book. What I'm trying to do is add an try-catch block inside my main function to catch an exception that is thrown by a function that I am using.
asked Apr 12 ummesalma 29.2k points
0 votes
2 answers 29 views
29 views
Problem: I want to know about Exception Handling in C++
asked Nov 2, 2020 john 580 points