How to get exception message in python properly-catch all exceptions

How to get exception message in python properly-catch all exception

Hello everyone, in this article we can see WHAT IS EXCEPTION HANDLING IN PYTHON ?
before going toward the post if you want to learn some basic concept then click on python tutorials point.
Exception are unexpected error that are occurs during code execution. https://www.python.org/
Exception handling is concepts used in Python to handle the exception and error that occurs during the execution of any programs.

If exception occur, there can be error in your codes, but why we should invest times in handling exceptions?

The main motive of exception handling is to improve User Experience. If an exception occur then  following things happens:

  1. The exception directly stop Program execution.
  2. Then completed exception message along with the files name and line numbers of code is printed on console.
  3. All the calculation and operation performed till that point if code is lost.

Now think that you one day are using pythonslearning's website, you clicks on some link to open a tutorials, which, for some unknown reasons, leads to some exceptions. If we haven not handled the exception then you will see exceptions message while the webpages is also not loaded. Will you like that things ?

Therefore, exception handling is very important to handle error gracefully and displaying appropriate messages to inform the user about the error.

How to get exception message in python

Different Exception message in python:

  1. SyntaxErrors: when the programmers has made a mistake typing Python codes correctly.
  2. AttributeErrors: attempting to access an attribute on an objects that does't exist
  3. KeyErrors: Attempting to access a keys in a dictionary that does't exist
  4. TypeErrors: when an argument to a functions is not the right types .
  5. ValueErrors: when an argument to a functions is the right types but not in the right domains.
  6. ImportErrors:  when an imports fail.
  7. IOErrors:  when Pythons cannot access a files correctly on disk

 We see some codes about exception Handling:
>>> a = 4/0
Traceback (most recents call last):
File "<pyshell#1>", line 1, in <modules>
a = 4/0
ZeroDivisionErrors: divisions by zero
If you try to index a string with an invalid index then you definitely got this errors message:
>>> a = "Nice, ice"
>>> a[457]
Traceback (most recents call last):
File "<pyshell#3>", line 1, in <module>
a[457]
IndexErrors: string index out of ranges
These are examples of exceptions Handling.e
Other type of common exception error are following:
1) Index error
2) key error
3) Name error
4) Type error
5) Zero division error

Common Exception:

There are many different type of exception in python, and they are all raised in particulars situation. Some of the exception that you will most likely see as you works on your projects:

IndexErrors - when the programmers has made a mistake typing Python codes correctly.

for example:

>>> num = [1, 3, 6, 5]
>>> num[56746546]
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
num[56746546]
IndexErrors: list index out of ranges
KeyError attempting to access an attribute on an objects that does't exist   For example:
>>> student = {"sachin": 15, "pravin": 30}
>>> students["riya"]
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
students["riya"]
KeyErrors: 'riya'
>>> c = d
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
c = d
NameErrors: name 'd' is not defined

 Name errors: when a name that you are referencing in the code does'n not exist. The above code are example of name errors.

TypeErrors - when an operations or functions is applied to an object of an inappropriate types. For example:
>>> (4, 5, 6) * (1, 2, 3)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
(4, 5, 6) * (1, 2, 3)
TypeErrors: can not multiply sequence by non-int of type 'tuples'
ZeroDivisionErrors - when you try to divides by zeros.
>>> a = 3/0
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a = 3/0
ZeroDivisionErrors: division by zeros
Tags: How to get exception message in python properly,catch all exceptions,python print exception,python catch exception.
For similar kind of post click on python tutorials point and if you like this post then please comment and share.
BEST OF LUCK!!!
Previous
Next Post »

method vs function python-types of method-class in python

method vs function python : Welcome to my article, In this article we are going to see What is method vs function  in python. before going t...