Manogna has Published 66 Answers

How to raise an exception in one except block and catch it in a later except block in Python?

Manogna

Manogna

Updated on 27-Sep-2019 11:27:53

Only a single except clause in a try block is invoked. If you want the exception to be caught higher up then you will need to use nested try blocks.Let us write 2 try...except blocks like this:try: try: 1/0 except ArithmeticError as e: if str(e) == "Zero division": print ("thumbs ... Read More

How to catch a thread's exception in the caller thread in Python?

Manogna

Manogna

Updated on 27-Sep-2019 11:26:40

The problem is that thread_obj.start() returns immediately. The child thread that you started executes in its own context, in its own stack. Any exception that occurs there is in the context of the child thread. You have to communicate this information to the parent thread by passing some message.The code ... Read More

How to catch an exception while using a Python 'with' statement?

Manogna

Manogna

Updated on 27-Sep-2019 11:25:28

The code can be rewritten to catch the exception as follows:try:      with open("myFile.txt") as f:           print(f.readlines()) except:     print('No such file or directory')We get the following outputC:/Users/TutorialsPoint1/~.py No such file or directoryRead More

Suggest a cleaner way to handle Python exceptions?

Manogna

Manogna

Updated on 27-Sep-2019 11:24:37

We can use the finally clause to clean up whether an exception is thrown or not:try:   #some code here except:   handle_exception() finally:   do_cleanup()If the cleanup is to be done in the event of an exception, we can code like this:should_cleanup = True try:   #some code here ... Read More

How to use the ‘except’ clause with multiple exceptions in Python?

Manogna

Manogna

Updated on 27-Sep-2019 10:51:43

It is possible to define multiple exceptions with the same except clause. It means that if the Python interpreter finds a matching exception, then it’ll execute the code written under except clause.In general, the syntax for multiple exceptions is as followsExcept(Exception1, Exception2, …ExceptionN) as e:When we define except clause in ... Read More

What is exception handling in Python?

Manogna

Manogna

Updated on 27-Sep-2019 08:06:16

An error is something that goes wrong in the program at the compile time like a syntactical error.For example.'abe' = 5OUTPUTSyntaxError: can't assign to literalEven if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution ... Read More

What is the use of "assert" statement in Python?

Manogna

Manogna

Updated on 27-Sep-2019 08:04:22

The assert statement has the following syntax.assert <some_test>, <message>The line above is read as: If <some_test> evaluates to False, an exception is raised and <message> will be output.If we want to test some code block or an expression we put it after an assert keyword. If the test passes or ... Read More

How to call a function with argument list in Python?

Manogna

Manogna

Updated on 27-Sep-2019 08:00:23

def baz1(foo, *args):The * next to args means "take the rest of the parameters given and put them in a list called args".In the line:foo(*args)The * next to args here means "take this list called args and 'unwrap' it into the rest of the parameters.in foo2, the list is passed ... Read More

How to pass arguments by value in Python function?

Manogna

Manogna

Updated on 27-Sep-2019 07:52:12

For given code the output is as followsb = 30 a = ['10']In this case, "a" seems to be passed by value, because the value is  unchanged even after the call to the function. So it is clear that the arguments have been passed by value in the python function.Read More

What are RuntimeErrors in Python?

Manogna

Manogna

Updated on 27-Sep-2019 07:03:31

A syntax error happens when Python can't understand what you are saying. A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions. This is called a run-time error because it occurs after the program starts running.A program or code may be ... Read More

Advertisements