Lesson 6 Try-except, catching errors

3 minutes
Share the link to this page
Copied
  Completed

Transcript

Minimum form of a try block, try block of code to test except a block of code to execute in case of error, or with an exception that can be caught and a message gets printed on the screen. We can also set it to bits of expressions. For cases where the same code must be interpreted for different errors like except i o error, value error. Therefore, we have several except statements. considering different errors that can happen and include a finally cross outs will allow us to execute an action. If no error occurs in the block.

This will be run in either the try not accept gifts a result are not built in exceptions. some exceptions contain other exceptions. For example, exception groups, all exceptions except keyboard interrupt, system, exit, and of course, base exception. Which groups are exceptions. The hierarchy is as follows. Base exception groups all built in exceptions, user defined exceptions should not inherit them directly, but inherit from exception system exit exception thrown by si s dot exit.

If it is not intercepted by an exception, the program stops. It does not inherit from exception, so that when the developer seeks to catch all possible errors, it does not prevent the program from quitting. Keyboard interrupt is stated when the user presses interrupt keys usually Control Policy like system exit, it inherits directly from base exception for the same reasons. Exception groups all exceptions that do not relate to program closer, your own exceptions must derive from it. It is usually used to ensure that everything worked well and if not to intercept and then display the error. that occurred customize exceptions, raising an exception, raise exception type message.

We say year as your input from the user, we try to convert the year to an integer, since it should not be a string or character. If it is less than zero, we give it an error message. If the conversion to integer failed, we send another message. Here's a class named my exception with two methods. I had it surrounded by double underscores takes reason as an argument. The str function is the one that is called when trying to display the instance of the exception.

I have so far called he here refers to reason. There's many function multiplier by five, which multiplies the argument and by five, provided that it is not greater than 20. Otherwise, the keyboard race is used to launch the exception. Doc string Duck string is a string placed just after the definition of a module, class, function or method, this string becomes the special doc attribute of the object, we have to find an additional function that returns the result of adding two numbers together. Writing doc strings offers many advantages. The Help function displays with documentation in a shell.

Programming tools, such as shells, or IDs. display this documentation when the developer who does not read your code, but uses it needs it, you can generate a good code doc with commands that extract these doc strings. We can also document modules by placing doc string as the first expression. You can type the doc or I have written here's a great module that will do many great things. We can document a class and its methods Also, you can write the doc for the cost After defining the class name, or After defining the method

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.