![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 27104 Articles for Server Side Programming
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
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 this way, we expect the same code to throw different exceptions. Also, we want to take the action in each case.Example codeimport sys try: d = 8 d = d + '5' except(TypeError, SyntaxError)as e: print sys.exc_info()We get output as shown(, TypeError("unsupported operand type(s) for +: 'int' and 'str'", ), ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
1K+ Views
Here we are passing a variable to given exception. We are defining a custom exception ExampleException which is a subclass of base class Exception and also defining the __init__ method. We use a try-except block to raise the exception and pass the variable to the exception as follows.Exampleclass ExampleException(Exception): def __init__(self, foo): self.foo = foo try: raise ExampleException("Bar!") except ExampleException as e: print e.foo Output"C:/Users/TutorialsPoint1/~bar.py" Bar!
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
203 Views
In given code, a custom exception FooException has been created which is a subclass of the super class Exception. We will pass a string object to the custom exception as followsExample#foobar.py class FooException(Exception): def __init__(self, text, *args): super ( FooException, self ).__init__ ( text, *args ) self.text = text try: bar = input("Enter a string:") if not isinstance(bar, basestring): raise FooException(bar) except FooException as r: print 'there is an error' else: print type(bar) print barIf this script is run at the terminal as follows we get$ python foobar.pyWe get the following if we enter a stringOutput"C:/Users/TutorialsPoint1/~foobar.py" Enter ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
516 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 syntactically correct and may not throw any syntax error. This code may still show error after it starts running.The given code can be corrected as followsa = input('Enter a number:') b = input('Enter a number:') c = a*b print cThe output we get is as follows"C:/Users/TutorialsPoint1/~.py" Enter a number:7 Enter ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
312 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 does not work in Python 3; now we should use ‘as’.The parentheses are necessary as the commas are used to assign the error objects to names. The ‘as’ keyword is for the assignment. We can use any name for the error object like ‘error’, ‘e’, or ‘err’Given code can be ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
282 Views
A substring is a contiguous sequence of characters within a string. In other words, it's a smaller piece of text that appears within a larger piece of text. For example, in the string "lorem ipsum", the substring "lorem" appears within the larger string. Similarly, the substring "em i" appears within the larger string "lorem ipsum" as well. Substring operations are common in programming and can be used for various tasks such as searching for a specific word in a text, extracting parts of a text, or replacing certain parts of a text with other words. Checking if a substring is ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
198 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 the expression evaluates to true nothing happens. But if the test fails or the expression evaluates to false, an AssertionError is raised and the message is printed out or evaluated.Assert statement is used for catching/testing user-defined constraints. It is used for debugging code and is inserted at the start of ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
454 Views
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: print "Cannot concatenate 'str' and 'int' objects"OutputCannot concatenate 'str' and 'int' objectsWe avoid the traceback error message elegantly with a simple message like above by using try except statements for exception handling.In addition to using an except block after the try block, we can also use the finally block. The ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
434 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 the start than line 2. All lines of code in a block must start with exactly the same number of spaces. Both print statements must be indented same number of spaces. So the corrected code that does not show unexpected indent is as follows.def a(): print "foo" print "baz"