Cara menggunakan python universal exception handler

In this article, we will discuss how to handle exceptions in Python using try. except, and finally statement with the help of proper examples. 

Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program. 

Difference between Syntax Error and Exceptions

Syntax Error: As the name suggests this error is caused by the wrong syntax in the code. It leads to the termination of the program. 

Example: 

Python3




# initialize the amount variable

amount

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
1

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
3

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
4

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
5
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
6
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
7
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
ZeroDivisionError Occurred and Handled
1
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

Output:

Cara menggunakan python universal exception handler

Exceptions: Exceptions are raised when the program is syntactically correct, but the code resulted in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.

Example:

Python3




# initialize the amount variable

ZeroDivisionError Occurred and Handled
4
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
1

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

ZeroDivisionError Occurred and Handled
8

ZeroDivisionError Occurred and Handled
9
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
ZeroDivisionError Occurred and Handled
4
NameError Occurred and Handled
2
NameError Occurred and Handled
3

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
NameError Occurred and Handled
5

Output:

Cara menggunakan python universal exception handler

In the above example raised the ZeroDivisionError as we are trying to divide a number by 0.

Note: Exception is the base class for all the exceptions in Python. You can check the exception hierarchy .  

Try and Except Statement – Catching Exceptions

Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

Example: Let us try to access the array element whose index is out of bound and handle the corresponding exception.

Python3




NameError Occurred and Handled
6

NameError Occurred and Handled
7

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

ZeroDivisionError Occurred and Handled
9
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
-5.0
a/b result in 0 
1
-5.0
a/b result in 0 
2
-5.0
a/b result in 0 
3
-5.0
a/b result in 0 
4
-5.0
a/b result in 0 
3
-5.0
a/b result in 0 
6
-5.0
a/b result in 0 
7

-5.0
a/b result in 0 
8
-5.0
a/b result in 0 
9

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
3
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
4
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
5
-5.0
a/b result in 0 
2
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
7

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
Can't divide by zero
This is always executed
0

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
Can't divide by zero
This is always executed
4
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
4
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
5
-5.0
a/b result in 0 
6
try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
7

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
0
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
5
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

Output

Second element = 2
An error occurred

In the above example, the statements that can cause the error are placed inside the try statement (second print statement in our case). The second print statement tries to access the fourth element of the list which is not there and this throws an exception. This exception is then caught by the except statement.

Catching Specific Exception

A try statement can have more than one except clause, to specify handlers for different exceptions. Please note that at most one handler will be executed. For example, we can add IndexError in the above code. The general syntax for adding specific exceptions are – 

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

Example: Catching specific exception in Python

Python3




Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
7

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
8

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
9

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

# initialize the amount variable1 # initialize the amount variable2

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
5 # initialize the amount variable5# initialize the amount variable6
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

# initialize the amount variable9amount0

# initialize the amount variable9amount2

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
ZeroDivisionError Occurred and Handled
9
NameError Occurred and Handled
2amount6amount7
-5.0
a/b result in 0 
6
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
02

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
06
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
07

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
08 

-5.0
a/b result in 0 
8
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
12
-5.0
a/b result in 0 
6
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
12
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
17
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
20

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
21

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
23

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
27
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
30

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
34
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

Output

ZeroDivisionError Occurred and Handled

If you comment on the line fun(3), the output will be 

NameError Occurred and Handled

The output above is so because as soon as python tries to access the value of b, NameError occurs. 

Try with Else Clause

In python, you can also use the else clause on the try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.

Example: Try with else clause

Python3




try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
36

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
9

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
38

# initialize the amount variable1

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
40

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
-5.0
a/b result in 0 
8
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

# initialize the amount variable9

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
45
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
47
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
48
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
49
NameError Occurred and Handled
2 amount6amount7
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
53

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
23

# initialize the amount variable9

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
60
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
63
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

# initialize the amount variable9

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
67

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
69

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
70
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
71
-5.0
a/b result in 0 
3
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
73
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
70
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
73
-5.0
a/b result in 0 
3
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
73
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

Output:

-5.0
a/b result in 0 

Finally Keyword in Python

Python provides a keyword finally, which is always executed after the try and except blocks. The final block always executes after normal termination of try block or after try block terminates due to some exception.

Syntax:

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)

Example:

Python3




try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
80

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
82

-5.0
a/b result in 0 
8
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
86
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
17
NameError Occurred and Handled
2
NameError Occurred and Handled
2
NameError Occurred and Handled
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
92

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
95

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
97

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
23

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
ZeroDivisionError Occurred and Handled
03
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

ZeroDivisionError Occurred and Handled
06
Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
1

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
ZeroDivisionError Occurred and Handled
09

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
ZeroDivisionError Occurred and Handled
11

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
ZeroDivisionError Occurred and Handled
15
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

Output:

Can't divide by zero
This is always executed

Raising Exception

The raise statement allows the programmer to force a specific exception to occur. The sole argument in raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception).

Python3




ZeroDivisionError Occurred and Handled
17

try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)

-5.0
a/b result in 0 
8
-5.0
a/b result in 0 
9

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
ZeroDivisionError Occurred and Handled
22
ZeroDivisionError Occurred and Handled
23
ZeroDivisionError Occurred and Handled
24
ZeroDivisionError Occurred and Handled
25
ZeroDivisionError Occurred and Handled
26

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
30

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
9
ZeroDivisionError Occurred and Handled
0
ZeroDivisionError Occurred and Handled
32
try:
    # statement(s)
except IndexError:
    # statement(s)
except ValueError:
    # statement(s)
8

try:
    # Some Code.... 

except:
    # optional block
    # Handling of exception (if required)

else:
    # execute if no exception

finally:
    # Some code .....(always executed)
0
ZeroDivisionError Occurred and Handled
22 
ZeroDivisionError Occurred and Handled
36

The output of the above code will simply line printed as “An exception” but a Runtime error will also occur in the last due to the raise statement in the last line. So, the output on your command line will look like 

Traceback (most recent call last):
  File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
    raise NameError("Hi there")  # Raise Error
NameError: Hi there

This article is contributed by Nikhil Kumar Singh(nickzuck_007) 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Apa itu Exception Handling python?

Python Exception handling adalah suatu mekanisme penanganan flow normal program karena terjadi exception dengan melanjutkan flow ke code block lainnya. Kenapa harus menangani exception? Karena terjadi exception dan kita tidak tangani, maka program akan berhenti.

Kapan kita menggunakan exception handling?

Exception Handling merupakan mekanisme yang paling diperlukan dalam menangani error yang terjadi pada saat runtime (program berjalan) atau yang lebih dikenal dengan sebutan runtime error. Secara umum, adanya kesalahan / error yang terjadi pada program pada saat runtime dapat menyebabkan program berhenti atau hang.

Bagaimana cara membuat penanganan kesalahan error handling yang baik pada program?

Cara Membuat Error Handling yang Baik.
Gunakan bahasa yang mudah dipahami oleh user..
Hindari kata-kata seperti : bad,dummy, dll..
Hindari kalimat perintah..
FAQ (Frequently Asked Questions)..
Optimalisasikan dan pemanfaatan undo redo function dan cancel..
Menyiapkan berbagai macam model respon..
Validitas masukan. /.

Apa penyebab terjadinya suatu pengecualian exceptions di suatu program?

Exception dipicu oleh runtime error, yaitu error atau kesalahan yang terjadi saat program dieksekusi oleh interpreter.