Welcome To
Exception


Exception In A Python

The exception is a one type of error that occurs during the runtime . when exception occurs the normal flow of execution is gets stop. and print the error message . in python provides the exception handlers like try and except. it prints the what type of error occurs in that particular code. and the it used to print the error message it prevent the crash of a program .

Some Reasons to occur a error that is in the below

  1. Input error
  2. Type error
  3. fileNotFound error
  4. Name Error
  5. Import error
  6. Indentation Error
  7. ZeroDivisionError
  8. Index Error
  9. Key Error
  10. IO Error

Built In Exceptions

The python provides the standard set of built in exception to raised the error during the run time . it prevent the code to crash a program

NameError

The raised exception when the no defined variable to print the value.

Error program

print(myName)

Correct program

myName="Darshan"
print(myName)

valueError

The Exception raised during the invalid input received. When the user to get the input value as wrong then exception raised value error

Error Program

int(" hello)

Correct program

str("Hello')

Type error

The exception raised when the value that variable can store the incorrect value

Error program

a=hello

Correct program

a="hello"

Indentation Error

The Indentation Error exception is raised when the python code having a incorrect indent as block of code

Error program

For i in range(1,10):
print(i)

Correct program

for i in range(1,10):
     print(i)

Key error

The Error exception raised when the key is not found in the existing dictionary.

Error Program

dist={"name"="darshan", "age"=19, 'place"="kolar"} print(address)

Correct Program

dist={"name"="darshan", "age"=19, 'place"="kolar"} print(place)

ZeroDivisionError

The Exception raised when we want to divide the any integer value ton zero . the ZeroDivisionError is appear

Error Program

print(10/0)

Correct program

print(10/5)

Index Error

The Error Exception raised when we want to access the invalid range of index in the existing sequence type.

Error Program

list=[1,2,3,4]
print(list[5])

Correct program

list=[1,2,3,4]
print(list[3])

IO Error

The python raised the error Exception when the file is not working properly or not found

Import Error

The error exception raised when the module name is wrong then the import cannot be work properly.

Exception Handling

Exception handling is a process or mechanism to handle the error may occur during the runtime . the exception make the program to execute continuously . and when the error occur it shows the informative of that particular error type .

Needs of Exception Handling

• Preventing crashes
• Provide the informative about error
• Continue for execution
• Handing external conditions

Exception Handling Using Try and Except

The Exception handling try and Except is a keywords to gather and raised the exception if error may occurs.
Try it is a keyword it contains a block of code that raises an exception.
Except It is also Keyword it Contains a type of exception it may occur. Small Example in the below

Syntax

try:
   # code for exception raised.
except Exceptiontype:
    # code of Exception type

Example

Try:
   list=[1,2,3,]
    print(list[10])
except IndexError:
   print("The index value does not exist")

Exception Handling Try,except, else and Finally

The Exception handling try and Except,else,finally is a keywords to gather and raised the exception if error may occurs.
Try it is a keyword it contains a block of code that raises an exception.
Except It is also Keyword it Contains a type of exception it may occur.
Else The else contain if error does not exist
Finally It contains a This block of code always executed

Syntax

try:
   # code for exception raised.
except Exceptiontype:
    # code of Exception type
else:
   # code of exception does not exist.

Example

Try:
   list=[1,2,3,]
    print(list[10])
except IndexError:
   print("The index value does not exist")
else:
   print("The code can execute with correct index")

Exception handling Using Try,Except, Else and Finally

The Exception handling try and Except is a keywords to gather and raised the exception if error may occurs.
Try it is a keyword it contains a block of code that raises an exception.
Except It is also Keyword it Contains a type of exception it may occur.
Else The else contain if error does not exist

Syntax

try:
   # code for exception raised.
except Exceptiontype:
    # code of Exception type
else:
   # code of exception does not exist.
finally:
   # the Code contain the block of code is always executed

Example

Try:
   list=[1,2,3,]
    print(list[10])
except IndexError:
   print("The index value does not exist")
else:
   print("The code can execute with correct index")
finally:
   print("The statement is always executed")

Raising Exception -- raise statement

The Raise is a keyword it is used to raise the exception manually

Syntax

raise Exceptiontype("error message")

Example

try:
   age=int(input('Enter your age'))
   if age<=0:
      raise ValueError("Age cannot be less Then 0")
    print(f'your age is{age}')
except ValueError as e:    print(e)