----BEGIN CLASS---- [14:02] #startclass [14:02] #rollcall [14:02] roll call : tabrez khan [14:03] Devesh Verma [14:03] pooja sulakhe [14:05] Cool, let's start [14:05] Today we are gonna discuss about Exceptions in Python, and how to handle them [14:06] Any error which happens during the execution of the code is an exception [14:06] Each exception generally shows some error message. [14:06] Piyush Aggarwal [14:07] There are various kinds of exceptions: NameError, TypeError, and lots more [14:07] NameError is a very common Exception that one can see when one starts writing code in Python [14:08] When you try to access a variable, which is not defined, you get a NameError [14:08] Try opening a Python shell and enter the following: [14:08] print(a) [14:09] You will see a `Traceback` message like `NameError: name 'a' is not defined` [14:10] The traceback message also contains useful info like in which line of your code, the error happened, along with the stacktrace [14:11] Next comes `TypeError` [14:11] This happens when someone tries to do an operation with different kinds of incompatible data types [14:12] To get a `TypeError`, you can enter the following line in Python shell [14:12] print(1 + "a") [14:13] This will throw a `Traceback` with a message like `TypeError: unsupported operand type(s) for +: 'int' and 'str'` [14:13] Any questions, so far? [14:13] ! [14:15] next [14:16] why the name is given as traceback? wny not trace forward [14:16] *why [14:17] traceback is a term used across programming languages [14:18] It shows you the past few stack trace leading to the exception [14:18] you are "tracing back" from the failed code to the problem that caused failure, maybe that's the point [14:19] since, it cannot show you the future calls your application is gonna make, I think it makes sense to call it `traceback`. This is my assumption [14:19] any other questions? [14:20] prokbird: https://en.wiktionary.org/wiki/traceback [14:21] We'll move forward [14:22] Exceptions are errors in your application and will cause the application to crash and exit, if not handled [14:23] Exceptions are not evil or bad. They are just exceptions. You will need to handle them. [14:24] Exceptions, if not handled in an application, GUI or server application, will lead to the application exiting and that may have some undesirable side effects: in memory data getting lost, bad user experience, etc. [14:24] So, let's look into how to handle exceptions [14:25] https://paste.fedoraproject.org/paste/FDujBA~3w4kRhcmvHnyQuQ [14:26] It works as follows: [14:27] First all lines between try and except statements are tried to be executed [14:27] If ExceptionName happens during execution of the statements then except clause statements execute [14:27] If no exception happens then the statements inside except clause does not execute. [14:28] If the Exception is not handled in the except block then it goes out of try block. [14:32] Example: try running this script: https://paste.fedoraproject.org/paste/B-R44UyGYxubhlX-g-wxIg [14:33] You can enter various values and see how the program behaves [14:34] Try entering a number 23, then a float: 45.1, then a string: 35,23 [14:35] in the first 2 cases, there will be no exception, but in the 3rd case, you will get a ValueError exception as it won't be possible to convert `35,23` to `float`. [14:36] Try pressing `Ctrl+c` to cause a `KeyboardInterrupt`. Since this Exception, is not handled, your program will exit. [14:38] To handle all exceptions, you can use just `except:` following multiple `except ` blocks [14:38] ! [14:38] This will ensure that you handle any unforeseen exceptions in your application and keep your application running. [14:38] next [14:39] the program does not catch the exception if i use valueError instead of ValueError [14:39] ? [14:40] how do i know which is to use? [14:40] prokbird: Read Python docs :P [14:41] These exception names are case sensitive just like any other variable names. [14:43] Now, let's look into how to raise exceptions [14:43] you raise exceptions by using `raise ExceptionName("some message")` [14:44] Exceptions provide a good way to communicate between functions when you run into errors. [14:44] Finally, comes the `finally` keyword. [14:45] If we want to have some statements which must be executed under all circumstances, we can use finally clause, it will be always executed before finishing try statements. [14:46] Check out this example: https://paste.fedoraproject.org/paste/ahh6QRrsukub4r9~thpEKg [14:46] In the last class on file handling, I told you that we should always close file objects, otherwise it causes a leak of file descriptors. [14:51] In the above shared example, we ensure that the open file object it closed, no matter what, in the case of error or no error [14:52] This covers the basics of exceptions in Python [14:52] ! [14:52] The more you write code, the more you will understand about exceptions and how to harness them. [14:53] The only way to become better at a task is to do it more and more. So, keep writing code. [14:53] next [14:53] i got disconnected meanwhile, ques is can we handle the keyboard interrupt ? [14:54] yes, you can [14:54] `except KeyboardInterrupt` [14:55] thanks ----END CLASS----