Sharp Tutorial
Exception in python

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

Introduction to Python

  • Introduction to Python
  • History of Python
  • How To Install Software
  • Variables in Python
  • Comments in Python
  • Input/Output
  • If else in Python
  • While Loop
  • For Loop

Collection in Python

  • Numbers in Python
  • List
  • Tuple
  • Set
  • Dictionary
  • Array
  • String

Object Oriented python

  • Function
  • Exception
  • OOPS (class/object)
  • Object Pass & Return
  • Constructor
  • Function Overloading
  • Module in Python
  • Inheritance
  • File Handling

Python With MYSQL And Excel

  • Connect MySql & Python
  • Insert Data MySQL
  • Delete/Update MySQL
  • Select Data From MySQL
  • Working With Excel in Python

Python GUI

  • GUI using Turtle
  • Introduction to Tkinter
  • Login Page using Tkinter
  • Data Grid Example Tkinter
  • Connect Multiple Forms
  • Database with GUI Python

Programs in Python

  • Swap two number
  • Calculate the area
  • Even Odd or Zero
  • Largest ,Middle and Smallest Number
  • Calculate Telephone Bill
  • Print Table of The given Number
  • Factorial of the number
  • Reverse and check number is palindrome
  • check number is prime , armstrong
  • Program to Print the given patterns
  • Guess A Number Game using Random
Click Here

Sharp (2) Tutorials

Exception Handling in Python

Exception is a run-time error that stops the execution of the program.

Python supports the following blocks for exception handling

1) try : This block is the prone for runtime error.
2) except: This block is used to handle the exception. This block executes only if there is an exception.
3) finally:This block always executes either there is exception or not.

try:
 a=10/0
 print(a)
except:
 print("exception occurred")

  Output:
  exception occurred

we can also use else which executes if there is no exception.

try:
 a=10/5
except:  print("exception occurred") else:   print(a)

   Output:
  2 (else excutes as there is no error).

Catch Specific Exception using multiple except:

try:
 a=int(input("enter first number")) 
b=int(input("enter second number"))
c=a/b
print(c)
except ZeroDivisionError:  print("Division By Zero Error")
except:
print("It catches all other exception not caught above")

   Output:
  if user enter 10 & 0  output:Division By Zero Error

 if User Enter ram in a or b it comes to general except and print It catches all……….

   

Nested try and except:

try:
 a=int(input("enter first number")) 
b=int(input("enter second number"))
try:
c=a/b
print(c)
except ZeroDivisionError:   print("Division By Zero Error for inner try")
except:
print("It catches all other exception not caught above and outer try block")

   Output:
  if user enter 10 & 0  output:Division By Zero Error for inner try

 if User Enter “ram” in a or b it comes to general except and print It catches all other exception……….

 

Example on finally:

try:
 a=10/0 # finally will always execute if error or not
except:
 print("exception occurred")
finally:
 print("this always executes")

   Output:
  exception occurred
  this always executes

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial