Much like that of Java,Ĭode that may or may not raise an exception should be placed in the try block. The try-except-finally block is used in Python programs to perform the exception-handling task. Inappropriate type was passed to a built-in operator or functionĪrgument error not covered by TypeError or a more precise error Referenced a non-existent mapping (dict) keyĪttempt to access a garbage-collected object Input() or raw_input() tried to read past the end of a file Raised when a floating-point operation failsĭivision or modulo operation with zero is divisorĪttribute reference or failure to assign correctly Raised by close() method of generators for terminating iteration This is the root exception for all others Lastly, if there is a specific type of exception that you’d like to throw that does not fit any of these, then you can write your own exception type object. Later in this chapter I will show you how you and them if you’d like. You can write any of these into a clause and try to handle them. The exception handling mechanisms within programming languages were developed for this purpose.īelow is a table of all exceptions that are built into the Python language along with a description of each. By nicely, I mean that the program will not abort and the end user will receive a descriptive error message stating what the problem is, and in some cases how it can be resolved. How often have you been working in a program and performed some action that caused the program to abort and display a nasty error message? It happens more often than it should because most exceptions can be caught and handled nicely. We’ll cover assert in depth here and learn the different ways that it can be used to help you out and save time debugging those hard-to-find errors. In Python as well as Java, the assert keyword can help out tremendously in this area. There are many different ways to debug and repair code we will go through some debugging methodologies in this chapter. Python has similar constructs to that of Java, and we’ll discuss them in this chapter.Īfter you have found an exception, or preferably before your software is distributed, you should go through the code and debug it in order to find and repair the erroneous code. Any Java programmer becomes familiar with exception handling on day one, as some Java code won’t even compile unless there is some form of exception handling put into place via the try-catch-finally syntax. Exception handling is all about ensuring that when your program encounters an issue, it will continue to run and provide informative feedback to the end-user or program administrator. There is no better way to frustrate an end-user then by having them run into an issue with your software and displaying a big ugly error message on the screen, followed by a program crash. Chapter 7: Exception Handling and Debugging ¶Īny good program makes use of a language’s exception handling mechanisms.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |