Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python KeyError Exceptions and How to Handle Them: Summary

You now know some common places where Python’sKeyError exception could be raised and some greatsolutions you could use to prevent them from stopping your program.

Now, the next time you see aKeyError raised, you will know that it is probably just a baddictionary key lookup. You will also be able to find all the information you need to determine where the error is coming from by looking at the last few lines of thetraceback.

If the problem is a dictionary key lookup in your own code, then you can switch from accessing the key directly on the dictionary to using the safer.get() method with a default return value. If the problem isn’t coming from your own code, then using thetryexcept block is your best bet for controlling your code’s flow.

Exceptions don’t have to be scary. Once you know how to understand the information provided to you in their tracebacks and the root cause of the exception, then you can use these solutions to make your programs flow more predictably.

Download

Sample Code (.zip)

3.3 KB

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member?Sign-In

Locked learning resources

The full lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member?Sign-In

00:00You now know some common places where Python’sKeyError exception could be raisedand some great solutions you could use to prevent them from stopping yourprogram. Nowthe next time you see aKeyError raised, you’ll know that it’s probably just a baddictionary key lookup.

00:15You will also be able to find all the information you need to determine wherethe error is coming from by looking at the last few lines of the traceback.

00:23If the problem is a dictionary key lookup in your own code,then you can switch from accessing the key directly on the dictionary to usingthe safer.get() method with a default return value.

00:33If the problem isn’t coming from your own code, then using thetry/exceptblock is your best bet for controlling your code’sflow. Exceptionsdon’t have to be scary. Once you know how to understand the information providedto you in their tracebacks and the root cause of the exception,then you can use these solutions to make your programs flow more predictably.

00:55So, that just about wraps up this short tutorial. I hope you enjoyed it,and thanks for watching!

Succinct and helpful.Really like Rich’s style of explanation.Learned something new!

Very easy to follow

Avatar image for Anonymous

Anonymous onNov. 29, 2019

Thorough & covers all the use cases.

Avatar image for bhumikalamba

bhumikalamba onDec. 10, 2019

Can we just use try and except even if it is our code. that’s easier than figuring out if get method exists or not for the particular data structure that I am using.

Useful course; thank you!

Become a Member to join the conversation.

Course Contents

Overview
100%

[8]ページ先頭

©2009-2026 Movatter.jp