Skip to main content

Exception Handling

Errors:
A programming mistake is said to be an error.

There are three types of errors:
1) Compile time errors
2) Run time errors
3) Logical errors

Compile time errors are also called syntax errors.
Run time errors are also called exceptions.

Example of syntax error:
i=0
if i==0 
   print(i)
In the above example we have missed the : symbol after if statement. It is called as syntax error.

Example of exception:
a=10
b=0
print(a//b)
In the above example ZeroDivisionError occurs. It is called as an exception.

This type of exceptions can be handled explicitly to display user friendly error messages.

Handling Exceptions:
Syntaxes:
1) try with except block:
    try:
         ========
    except ExceptionClassName:
        ========

2) try with multiple except blocks:
    try:
         ==========
    except ExceptionClassName1:
        ===========
    except ExceptionClassName2:
        ===========

3) Multiple exceptions in a single block:
    try:
         ==========
    except(ExceptionClassName1, ExceptionClassName2, ........)
         ==========

4) except block without exception class name
    try:
        ==========
    except:
        ==========

Examples:
1) 
try:
    a=int(input("Enter First Number: "))
    b=int(input("Enter Second Number: "))
    c=a//b
    print(c)
except ZeroDivisionError:
    print("Enter Second Number Except Zero")

2)
try:
    a=int(input("Enter First Number: "))
    b=int(input("Enter Second Number: "))
    c=a//b
    print(c)
except ValueError:
    print("Enter Numbers Only")
except ZeroDivisionError:
    print("Enter Second Number Except Zero")

3) 
try:
    a=int(input("Enter First Number: "))
    b=int(input("Enter Second Number: "))
    c=a//b
    print(c)
except(ZeroDivisionError, ValueError):
    print("Enter two numbers and second number except zero")

4) 
try:
    a=int(input("Enter First Number: "))
    b=int(input("Enter Second Number: "))
    c=a//b
    print(c)
except:
    print("Enter two numbers and second number except zero")

Popular posts from this blog

Python Fundamentals

Python: Python is an interpreted, high level, general purpose programming language. Differences between compiler and interpreter 1) Compiler converts whole program at a time where as Interpreter conver line by line 2) Compiler generates a file where as Interpreter does not generates a file 3) Compiler is fast where as Interpreter is slow Both Compiler and Interpreter are translation softwares. Whenever we run python program, internally source code converted into byte code by python compiler, byte code converted into bit code by python virtual machine and bit code executed under operating system to get the output. Python supports scripting, structured programming, modular programming and Object Oriented Programming. Applications of python: 1) Artificial Intelligence & Machine Learning Applications 2) Data Science & Data Visualization Applications 3) Web Scrapping Applications 4) Scientific & Numeric Applications 5) IOT(Internet Of Things) Applic...

Control Flow Statements

Control Flow Statements are divided into three categories in Python: 1) Decision Making Statements 2) Iteration Statements(Loops) 3) Jump Statements 1) Decision Making Statements: Decision making statements contain conditions. If the condition is true then a set of statements executed and if the condition is false then another set of statements are executed. Decision making statements are also called selection statements. i) if Statement ii) if else Statement iii) if elif ....... else Statement iv) Nested if Statement 2) Iteration Statements(Loops): A set of statements executed repeatedly until the condition becomes false is called as iterative statement or loop. i) while loop ii) while loop with else block iii) for loop iv) for loop with else block v) Nested loops 3) Jump Statements: Jump statements are used to terminate the loop or to skip the part of a loop. i) break Statement ii) continue Statement iii) pass Statement Assignment4: ...
Core Python Advanced Python By Venkatesh Mansani venkatesh.mansani@yahoo.com