Try and Exception in Python

Try and Exception in Python

  • Exception is the error and is an event, which occur when program statements are executing.
  • It disrupts the flow of program’s statements.
  • Exception is the run time error when occur, further program is not execute.


Exception List

StopIteration

SystemExit

StandardError

ArithmeticError

OverflowError

FloatingPointError

ZeroDivisionError

AssertionError

AttributeError

EOFError

ImportError

KeyboardInterrupt

LookupError

IndexError

KeyError

NameError

UnboundLocalError

EnvironmentError

IOError

SyntaxError

IndentationError

SystemError

SystemExit

TypeError

ValueError

RuntimeError

NotImplementedError


Exception Handling: try except else and finally:

try: Used to test a code or to try a code whether it give error or not.

except: Used to handle a error

else: Used to execute code when code is error free

finally: It execute a code after try except and /or else part. It does not mean with raising error or not. It will excecute when try except to end.?


Example: try except

Example with any exception:

try:

? print(name)

except:

? print(“Exception will be raise, because name is not defined")

Specify only one exceptions

try:

? print(3/0)

except ZeroDivisionError:

? print(“You are dividing with zero, that is error")



Specify multiple exceptions

try:

? print(3/0)

except ZeroDivisionError:

? print(“You are dividing with zero, that is error")

except SyntaxError:

??print(“There is syntax error”)

except:

??print(“There is another error occurred”)


Example: try except and else

Example with any exception:

try:

? print(name)

except:

? print(“Exception will be raise, because name is not defined")

Else:

? print(“Not a error because variable is defined”)


Example: try except else and finally

Example with any exception:

try:

? print(name)

except:

? print(“Exception will be raise, because name is not defined")

else:

? print(“Not a error because variable is defined”)

finally:

??print(“Code is completed”)




要查看或添加评论,请登录

Rushikesh J.的更多文章

  • Python Array With Examples

    Python Array With Examples

    Array in Python Array in Python Array is the collection of items having same data type with contiguous memory location.…

  • Python Date Object

    Python Date Object

    We can work with time and date with Python. There are two module in Python time and datetime that used to work with…

  • String Formatting WITH Problems and Solution

    String Formatting WITH Problems and Solution

    What is String Formatting? It is the way to insert custom string or variable in a text (string). Using string…

  • SET Different Methods With Examples

    SET Different Methods With Examples

    SET Method : add() Working: This method is used to add element to set. Syntax: set.

  • Python SET Comprehension With Examples

    Python SET Comprehension With Examples

    What is Comprehension? ?We create a sequence from a given sequence is called comprehension. ?Sequence means list…

  • Difference And Symmetric Difference SET Method With Exercises

    Difference And Symmetric Difference SET Method With Exercises

    SET : Difference Method Working: It return element that are present in first set but same element absent in 2nd set. if…

  • SET : Union Method With Example

    SET : Union Method With Example

    Working: Union method will exclude all the duplicate elements / items in a sets It combine all the items of two or many…

    1 条评论
  • Introduction To Python SET Data Type

    Introduction To Python SET Data Type

    SET is the collection of unordered, unchangeable and unindexable items SET is the mutable data type, because we can add…

  • Python Dictionary Methods

    Python Dictionary Methods

    Python Dictionary Method 1 : clear() Working: It used to remove all the elements from dictionary Syntax: dict.clear()…

  • Python Dictionary Comprehension

    Python Dictionary Comprehension

    What is Comprehension ?We create a sequence from a given sequence is called comprehension ?Sequence means list, tuple…

社区洞察

其他会员也浏览了