Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default message in custom exception - Python

I want to create a custom exception in Python, that when raised without any arguments, it will print a default message.

Code Example:

class CustomException(Exception):     pass # some code  raise CustomException() 

and get the below output:

Traceback (most recent call last):   File "<stdin>", line 1, in <module> __main__.CustomException: This is a default message! 
like image 788
pgmank Avatar asked Mar 11 '18 20:03

pgmank


People also ask

What is the default exception in Python?

Default Except Block in python We can use default except block to handle any exceptions; In the default except block generally, we can print exception information to the console.

How do you customize an exception in Python?

Creating Custom Exceptions In Python, users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from the built-in Exception class. Most of the built-in exceptions are also derived from this class.

How do I get the exact exception message in Python?

To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e . You can now print the error message with "print(e)" or use it for further processing.

How do I create a custom exception message?

Steps to create a Custom Exception with an ExampleCreate one local variable message to store the exception message locally in the class object. We are passing a string argument to the constructor of the custom exception object. The constructor set the argument string to the private string message.


2 Answers

This is the simplest solution IMHO how to define custom exception with a default message that can be overridden if needed:

class CustomException(Exception):     def __init__(self, msg='My default message', *args, **kwargs):         super().__init__(msg, *args, **kwargs) 

Usage example:

In [10]: raise CustomException --------------------------------------------------------------------------- CustomException                           Traceback (most recent call last) <ipython-input-10-259ae5202c8e> in <module> ----> 1 raise CustomException  CustomException: My default message  In [11]: raise CustomException() --------------------------------------------------------------------------- CustomException                           Traceback (most recent call last) <ipython-input-11-c1921a8781a6> in <module> ----> 1 raise CustomException()  CustomException: My default message  In [12]: raise CustomException('Foo bar') --------------------------------------------------------------------------- CustomException                           Traceback (most recent call last) <ipython-input-12-7efbf94f7432> in <module> ----> 1 raise CustomException('Foo bar')  CustomException: Foo bar 
like image 175
illagrenan Avatar answered Sep 19 '22 19:09

illagrenan


The solution is given by the bellow code:

class CustomException(Exception):     def __init__(self, *args, **kwargs):         default_message = 'This is a default message!'          # if any arguments are passed...         # If you inherit from the exception that takes message as a keyword         # maybe you will need to check kwargs here         if args:             # ... pass them to the super constructor             super().__init__(*args, **kwargs)         else: # else, the exception was raised without arguments ...                  # ... pass the default message to the super constructor                  super().__init__(default_message, **kwargs) 

An equivalent but more succinct solution is:

class CustomException(Exception):     def __init__(self, *args, **kwargs):         default_message = 'This is a default message!'          # if no arguments are passed set the first positional argument         # to be the default message. To do that, we have to replace the         # 'args' tuple with another one, that will only contain the message.         # (we cannot do an assignment since tuples are immutable)         # If you inherit from the exception that takes message as a keyword         # maybe you will need to check kwargs here         if not args: args = (default_message,)          # Call super constructor         super().__init__(*args, **kwargs) 

An even more succinct but restricted solution, in a way that you can only raise the CustomException with no arguments is:

class CustomException(Exception):      def __init__(self):          default_message = 'This is a default message!'          super().__init__(default_message) 

You can of course save one line, in each of the above solutions, if you just pass the string literal to the constructor rather than using the default_message variable.

If you want the code to be Python 2.7 compatible, then you just replace the: super() with super(CustomException, self).

Now running:

>>> raise CustomException 

will output:

Traceback (most recent call last):   File "<stdin>", line 1, in <module> __main__.CustomException: This is a default message! 

and running:

raise CustomException('This is a custom message!') 

will output:

Traceback (most recent call last):   File "<stdin>", line 1, in <module> __main__.CustomException: This is a custom message! 

This is the output that the first 2 solutions' code will produce. The last solution, differs in that calling it with at least one argument, like:

raise CustomException('This is a custom message!') 

it will output:

Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: __init__() takes 1 positional argument but 2 were given 

because it does not permit any arguments to be passed to the CustomException when it is raised.

like image 35
pgmank Avatar answered Sep 17 '22 19:09

pgmank