What is the difference between 'except Exception as e' and 'except Exception, e' in Python?

Rajendra Dharmkar
Published on 07-Dec-2017 11:18:19
The difference between using ', ' and 'as' in except statements, is as follows:Both ', ' and 'as' are same functionality wise; but their use depends on the python versions as follows.In Python 2.5 and earlier versions, use of the 'comma' is recommended since 'as' isn't supported.In Python 2.6+ versions, ... Read More

What is unexpected indent in Python?

Manogna
Published on 07-Dec-2017 11:06:45
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

What is exception handling in Python?

Manogna
Published on 07-Dec-2017 11:04:12
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

Explain try, except and finally statements in Python.

Manogna
Published on 07-Dec-2017 11:00:49
In exception handling in Python, we use the try and except statements to catch and handle exceptions. The code within the try clause is executed statement by statement.If an exception occurs, the rest of the try block is skipped and the except clause is executed.Exampletry: 'apple' + 6 except Exception: ... Read More

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

Manogna
Published on 07-Dec-2017 10:56:45
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 handle an exception in Python?

Manogna
Published on 07-Dec-2017 10:54:44
The simplest way to handle exceptions in python is using the "try-except" block.Exampletry: fob = open("test.txt", "r") fob.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find the file or read data" else: print "Write operation is performed successfully on the file" fob.close()OutputError: can't find the ... Read More

How to catch KeyError Exception in Python?

Manogna
Published on 07-Dec-2017 10:53:13
A KeyError is raised when a value is not found as a key of a dictionary. The given code is rewritten as follows to catch the exception and find its type.Exampleimport sys try: s = {'a':5, 'b':7}['c'] except: print (sys.exc_info())Output(<type 'exceptions.KeyError'>, KeyError('c', ), <traceback object at  0x0000000003203748>)Read More

What are the best practices for exception handling in Python?

Manogna
Published on 07-Dec-2017 10:47:11
Some of the best practices for exception handling in python are given below:Exceptions are better than returning error status codes. We have to handle exceptions in Python as the whole language core and standard libraries throw exceptions. Elegantly handled exceptions are any day preferable to error codes and trace backs.It ... Read More

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

Manogna
Published on 07-Dec-2017 10:45:38
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

What are RuntimeErrors in Python?

Manogna
Published on 07-Dec-2017 10:43:14
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