Manogna has Published 40 Articles

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

Manogna

Manogna

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

434 Views

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

56 Views

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 directory

Suggest a cleaner way to handle Python exceptions?

Manogna

Manogna

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

105 Views

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

2K+ Views

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 the use of "assert" statement in Python?

Manogna

Manogna

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

179 Views

The assert statement has the following syntax.assert , The line above is read as: If evaluates to False, an exception is raised and 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 pass arguments by value in Python function?

Manogna

Manogna

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

562 Views

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.

What are RuntimeErrors in Python?

Manogna

Manogna

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

485 Views

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

What is unexpected indent in Python?\\\

Manogna

Manogna

Updated on 26-Sep-2019 19:56:53

411 Views

Python not only insists on indentation, it insists on consistent indentation. If we indent one line by 4 spaces, but then if we indent the next by 3 (or 5, 6, .), we get this error of unexpected indent in python.In the given code, line 3 has more spaces at ... Read More

How to catch multiple exceptions in one line (except block) in Python?

Manogna

Manogna

Updated on 26-Sep-2019 19:52:43

291 Views

We catch multiple exceptions in one except block as followsAn except clause may name multiple exceptions as a parenthesized tuple, for exampletry: raise_certain_errors(): except (CertainError1, CertainError2, …) as e: handle_error()Separating the exception from the variable with a comma still works in Python 2.6 and 2.7, but is now deprecated and ... Read More

Why does Python code run faster in a function?

Manogna

Manogna

Updated on 30-Jul-2019 22:30:20

313 Views

It is found that if python code is run normally and then if it is run in a python function, it runs faster in the latter case. I want to know why python code runs faster in a function.It is generally found that it is faster to store local variables ... Read More

Advertisements