
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to write custom Python Exceptions with Error Codes and Error Messages?
We can write custom exception classes with error codes and error messages as follows:
class ErrorCode(Exception): def __init__(self, code): self.code = code try: raise ErrorCode(401) except ErrorCode as e: print "Received error with code:", e.code
We get output
C:/Users/TutorialsPoint1/~.py Received error with code: 401
We can also write custom exceptions with arguments, error codes and error messages as follows:
class ErrorArgs(Exception): def __init__(self, *args): self.args = [a for a in args] try: raise ErrorArgs(403, "foo", "bar") except ErrorArgs as e: print "%d: %s , %s" % (e.args[0], e.args[1], e.args[2])
We get following output
C:/Users/TutorialsPoint1/~.py 403: foo , bar
- Related Articles
- Error Correcting Codes - Hamming codes
- Error Correcting Codes - Reed-Solomon codes
- Error-Detecting Codes - Parity
- Error-Detecting Codes - Checksums
- Exceptions and Error in PHP 7
- What are Error-Detecting Codes?
- Error Correcting Codes - Low-Density Parity Check Codes
- Error correcting codes in Computer Networks
- Error Correcting Codes - Binary Convolutional Code
- Why are Python exceptions named "Error" (e.g. ZeroDivisionError, NameError, TypeError)?
- Error codes, cause and example of deadlock in DB2
- Error-Detecting Codes - Cyclic Redundancy Checks (CRCs)
- Explain the Hamming Codes in Error Correction
- Explain the Error Reporting Messages in ICMP Protocol
- Error codes returned in the PositionError object HTML5 Geolocation

Advertisements