

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I manually throw/raise an exception in Python?
We use the most specific exception constructor that fits our specific issue rather than raise generic exceptions. To catch our specific exception, we'll have to catch all other more specific exceptions that subclass it.
We should raise specific exceptions and handle the same specific exceptions.
To raise the specific exceptions we use the raise statement as follows.
Example
import sys try: f = float('Tutorialspoint') print f raise ValueError except Exception as err: print sys.exc_info()
output
We get the following output
(<type 'exceptions.ValueError'>, ValueError('could not convert string to float: Tutorialspoint',), <traceback object at 0x0000000002E33748>)
We can raise an error even with arguments like the following example
Example
try: raise ValueError('foo', 23) except ValueError, e: print e.args
output
We get the following output
('foo', 23)
- Related Questions & Answers
- How do I raise an http error/exception from a Python CGI script?
- How to raise an exception in Python?
- How can I get a JavaScript stack trace when I throw an exception?
- How do you throw an Exception without breaking a for loop in java?
- Can a constructor throw an exception in Java?
- How to throw a C++ exception?
- Throw Custom Exception in Kotlin
- How to raise Python exception from a C extension?
- How to throw custom exception in Kotlin?
- How to throw an exception from a static block in Java?
- How can an exception be thrown manually by a programmer in java?
- How can I trigger an onchange event manually in javascript?
- Can the abstract methods of an interface throw an exception in java?
- toDataURL throw Uncaught Security exception in HTML
- How can I manually set proxy settings in Python Selenium?
Advertisements