Python 3 Programming Tutorial - Try and Except error Handling

preview_player
Показать описание
In this Python 3 programming tutorial, we cover the Try and Except statements, which are used for error handling. These statements work similarly to the if-else, where if the Try runs, the except will not run. If the Try fails, then the exception will run with the error that was just generated in the try.

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Рекомендации по теме
Комментарии
Автор

Thanks, everyone else just gives the Try Exception format without explaining the importance of the programmer thinking ahead and employing something like an If-Else statement. Very good

sofoboachie
Автор

x = 0
y = 2

try:
print(y/x)

except Exception as e:
print("Error in your code " + str(e))

fedora
Автор

Everything works fine till I add the if, else. After that the problem is if I enter say GreeN even though we have
the whatColor.lower()) to allow the use of upper case or lower case, it doesn't work for me. Again when I
use the if else, other wise it all works fine. Any help as to why?

cybernoodleowww
Автор

Can anybody explain the raise keyword?

TheGamezAM
Автор

my python program shows "inconsistent use of tabs and spaces in indentation" please help!

mohammadummair
Автор

Hello, Sentdex, do you know why excepton function in the code write as below cannot work? I input word not in the list, however, there is no exception posted. Thank you.

y=["s", "A", "siri", "b", "a"]
try:
word=input("input the word you want:\n")
if word in y:
indexof=y.index(word)
print(word)
# else:
# print("word not in the list")
except Exception as e:
print(e)

mango_
Автор

how does he get 'pink' is not on the list? I have only "continuing". Absolute beginner.

Kisuke
Автор

So I tried to set up data validation to test to see if the user input was a valid int or float. The only way I got it to work was with the try / except. I even tried the isinstance to return true or false but because user input is automatically set as string it didnt work. Any help would be great, as of right now I'm converting the user I put into float and doing the try except on the value error because letters cant be converted to float.

gwbrown
Автор

@Sentdex please post your ERC20 address up. Your videos are some of the most helpful and comprehensive education tools I've come across and you deserve to be rewarded :)

Hellomynamesolly
Автор

Hi, if the else is triggered and the program outputs 'color not found or is not a color', is there a command (like go to) that we can use to jump back to the input line in the code and ask the user to input the color again? (and do this in a loop, until the user inputs something correct)

farzadk.
Автор

at the 0:29 you have ValueError, but why do you use NameError for exception?

secondmodu
Автор

Heres my source code:
hi = int(input("Input a number "))
yo = int(input("Input a second number "))


try:
print(hi + yo)

except Exception as e:
print(e)

print('continuing')



How can i make it so that this program doesn't crash when I enter a string in one of the values?

coltonvevomixtapes
Автор

can you make a VIDEO about assertion please.

b
Автор

Eh! Stop Smashing your keyboard and Let the dog sleep!  2:35
Amazing tutorial as always :)

pythonprogrammer
Автор

can you use a ide with a darker theme? just like pycharm or sublime. because the white theme of your ide makes my eyes burn, probably not just me

Zed
Автор

for some reason i got that error
['20-02-16', '21-08-19']
['red', 'blue']
What colour date of do you want to know?red
<built-in method lower of str object at is not in list
continuing thanks to exception

it works ok without expection


try:
whatColor = input("What colour date of do you want to know?")
coldex = colors.index(whatColor.lower)
theDate = dates[coldex]
print("The date of", whatColor, "is a", theDate)
'assignes colour to date'
"lower changes letters to lowercase"
except Exception as e:
print(e)
print("continuing thanks to exception")

uceee
Автор

except(OverflowError, ValueError):

except TypeError:

except AttributeError:
...

GPCTM
Автор

May I know what is the meaning of e?sorry, I'm new in python.

aaronsiew
Автор

Hi i am getting error like this
Traceback (most recent call last):
File "csv", line 16, in <module>
whoseSal = input("whose salary you want to know ?")
File "<string>", line 1, in <module>
NameError: name 'Rajiv' is not defined

Given CSV file Data:
Srinu, IND, Analyst, 50000
Rajiv, CHI, Prorammer, 75000

Chintu, USA, Admin, 40000
Hara, JAP, Associate, 50000

while running this code

import csv
with open("empd.csv") as csvfile:
csvread = csv.reader(csvfile, delimiter=', ')
names = []
sals = []

for row in csvread:
name = row[0]
sal = row[3]
names.append(name)
sals.append(sal)

print(names)
print(sals)

whoseSal = input("whose salary you want to know ?")
nameedx = names.index(whoseSal)

the_sal = sals[nameedx]
print("salary of ", whosesal, "is ", the_sal)


Can you help me to solve this problem.

laxminarasimhasrinivas
Автор

mr. sentdex why did you choose win 10, i like win 8.1 better though

kurisu