0% found this document useful (0 votes)
37 views10 pages

Nama: Prado Pratama Putra NIM: 09030581923047 Kelas: Tk4A

This document discusses exception handling in Python. It explains that exceptions occur when something goes wrong in a program due to incorrect code or input. Common exceptions include ImportError, IndexError, NameError, SyntaxError, and TypeError. The try/except statement is used to handle exceptions - code in the try block might throw an exception, and if it does, the code in the except block will run instead. Finally blocks ensure code runs no matter what errors occur. Exceptions can also be raised manually using the raise statement. Assertions are used for sanity checks and raise exceptions if a test fails. Files can be opened in different modes to read from or write to them.

Uploaded by

Prado Pratama
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
37 views10 pages

Nama: Prado Pratama Putra NIM: 09030581923047 Kelas: Tk4A

This document discusses exception handling in Python. It explains that exceptions occur when something goes wrong in a program due to incorrect code or input. Common exceptions include ImportError, IndexError, NameError, SyntaxError, and TypeError. The try/except statement is used to handle exceptions - code in the try block might throw an exception, and if it does, the code in the except block will run instead. Finally blocks ensure code runs no matter what errors occur. Exceptions can also be raised manually using the raise statement. Assertions are used for sanity checks and raise exceptions if a test fails. Files can be opened in different modes to read from or write to them.

Uploaded by

Prado Pratama
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 10

BAB 5&6

NAMA : PRADO PRATAMA PUTRA


NIM : 09030581923047
KELAS : TK4A
EXEPSION & FILES
You have already seen exceptions in provious code .
They occur when something goes wrong , due to
incorrect code or infut . When an exception occurs the
program immediately stops.
The following code produces the zero devision error
exception by trying to devide 7 by 0.
Exeption
Differrent exceptions are raised for different reasons
Common exception :
Importerror : an import fails ;
Indexerror: alist is indexed with an out-of-range number;
NameError:an unknown variable is used;
syntaxError: the code can’t be parsed properly;
typeError: a function is called on a value of an inappropriate
type;
Value Error: a function is called on a value of the correct type
,but with an inappropriate
value
Exception handling
Towhen handle exceptions ,and to call code when an exception occurs ,you
can use a try/except statement
The try block contains code that mght throw an exceoptionif that exception
occurs, the code in the try stops being execude ,and the code in the except
block is run if no error occurs , the code in the except block doesn’t run .
Ex :
Varieabel = 10
Print (10/2)
Except zerodeviasionError:
Print(“Error”)
Print (“finished”)
A try statement can have multiple different except blocks
to handle different exceptions
Multi exceptions can also be put into a single except block
using parentheses ,to the except block handle all of them
Example :
Try : variable = 10
Print(variable + “hello”)
Print (“variable /2”)
except zero devisionError:
Print(“dividded by zero”)
Except(valueError , typeError):
Print(“error occourred”)
FINNALY
To ensure come code runs no metter what errrors occur, you
can use a finally statement .the finally
Statement is placed at the bottom of a try /except statement.
Code withina finally statement always
Runs after excution of the codein the try ,and possibly in the
except , block .
Try :
print(“hello’)
print(“1/2”)
Except zerodevisionError:
print(“divided by zero”)
Print (“this code will be run no metter what “)
Code in finally statement even runs if uncaught
exception occurs in one of the preceding blocks.
Example :
Try:
print(1)
print(“10/0”)

Except zerodevisionError:
print(unknowv-var)
Finally :
Print(“this is excused last”)
RAISING EXEPTION
You can exceptions by using the raise statement .
Example :
Print (1)
raise valueError
print(2)
Exceptions can be raised with arguments that give detail about them
Name = “123”
raise nameError (“invali name!”)
In except blocks , the raise statement can be used without
arguments to re –raise whatever
Exception occured
ASSERTION
An assertion is a sanity-check that you can turn on or turn of
when you have finished testing the program.
an axpression is tested, and if the result comes up false, an
exception is raised
Assertions are carried out through use of the assert
statement .
Print(1)
assert 2+2 =4
Print(2)
Assert 1+1=3
Print (3)
Opening files
You can use python to read and write the contents of
files text files are easiest to manipulate ,bepore a file
can be edited , it must be opened,using the open
function
You can specify the mode

You might also like