EYR Python - Encapsulation 6 Exception

EYR Python - Encapsulation 6 Exception


EYR Python - Encapsulation 6 Exception

Introduction:
Talking about how we can handle an exception

Source Code: https://github.com/kaixing2012/How-I-

Python Exception Page: https://docs.python.org/3/library/exc


#Python
#Encapsulation
#Exception
#台灣PythonProgrammer
#軟體工程


Content

2.56 -> Hey guys, how you doing? Today, we are gonna be talking about exception. So what's the exception?
9.019 -> Exception in programming is used to detect if something...there is something wrong in a program
15.519 -> So let me show you why sometimes we should use exception
20.56 -> Here I have one
22.72 -> if statement and I'm trying to check
25.68 -> if the action of the new value assigning to
30.88 -> the
31.84 -> The new name assigning to first name was successfully finished if it is positive
38.64 -> print true. Otherwise, it prints false. So let me run it
43.52 -> however, we are getting
45.68 -> a syntax error, which is invalid syntax
49.76 -> The problem is here. We cannot
52.399 -> put a new value assignment within the bracket of
56.96 -> If statement, it doesn't make any sense to the python system.
63.199 -> so
64.559 -> But...however, we still have to check. So to fix this one, we have to use exception for instead
72.96 -> Let me show you how we can do it
75.28 -> so within the
77.36 -> first name setter
79.28 -> uh
80.4 -> It is returning true or false here
83.92 -> However, it is no longer useful for
88.159 -> if statement so we are now going to raise
92.159 -> an exception here and here I have the official website of python 3 opened
98.64 -> And it is in exception page
102.159 -> So there are a lot of exception here
105.04 -> But the only one exception that we are going to use is ValueError. So I'm going to copy it
113.84 -> and I'm going to raise
117.439 -> raise
118.64 -> ValueError here
120.479 -> and I'm going to leave a message. For example, given
124.799 -> value
127.36 -> Can only be
131.68 -> language
135.96 -> character
138.8 -> And instead of returning true, I'm going to print the value
148.4 -> Is changed
150.959 -> Okay, and
154.239 -> To catch the exception we have to use try and except block. So let me show you how we can do
163.92 -> I'm going to use try this action
167.68 -> So I'm going to get rid of this one and instead of else, I'm going to put except
176.64 -> With the ValueError
180.319 -> error...error yep, as v, I'm going to print v
187.36 -> And if I run it returns given value can only be language character
192.56 -> Which means we are putting one which is a numeric value which is in...dosen't make any...
200.4 -> which is not matching the pattern that we want, so if I change to A
206.879 -> And run it again
208.319 -> The value is changing. So it makes any sense. It makes a lot of sense to the program itself
215.44 -> And I think that's for today
218.48 -> And this is how we can handle exception. I'll see you next time

Source: https://www.youtube.com/watch?v=ORON5c2vVCg