{"id":25668,"date":"2021-12-04T09:19:07","date_gmt":"2021-12-04T03:49:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=25668"},"modified":"2021-12-04T09:19:07","modified_gmt":"2021-12-04T03:49:07","slug":"exceptions-and-errors-in-python-different-exceptions-and-their-handling-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/exceptions-and-errors-in-python-different-exceptions-and-their-handling-in-python\/","title":{"rendered":"Exceptions and Errors\u00a0in Python – Different Exceptions and their Handling in Python"},"content":{"rendered":"
When we write a program, we can make mistakes that cause errors when we run it. When a Python program meets an unhandled error, it terminates. These errors can be divided into two categories:<\/p>\n
Syntax Errors:<\/strong><\/p>\n Syntax error or parsing error refers to an error caused by not following the right structure (syntax) of the language.<\/p>\n For Example:<\/strong><\/p>\n Output:<\/strong><\/p>\n An arrow in the example indicates where the parser encountered the syntax problem.<\/p>\n We can see that there is no colon: in the if statement in line 2<\/p>\n Logical Errors(Exceptions):<\/strong><\/p>\n Exceptions or logical errors are errors that occur at runtime (after passing the syntax test).<\/p>\n An exception is a condition in a program that interrupts the flow of the program and causes the code to stop running. Python has a fantastic approach to handling exceptions so that the code runs without problems or disruptions.<\/p>\n For example, they occur when we attempt to open (read) a file that does not exist (FileNotFoundError), divide an integer by zero (ZeroDivisionError), or import a module that does not exist (ImportError).<\/p>\n Python generates an exception object if these types of runtime issues occur. If the error is not handled appropriately, it prints a traceback to the error as well as some information about why the issue happened.<\/p>\n Exceptions can be either built-in errors\/exceptions or bespoke exceptions.<\/p>\n The following are some examples of common in-built exceptions:<\/p>\n For Example:<\/p>\n Output:<\/strong><\/p>\n We use try-except statements to avoid errors\u00a0from occurring and halting the program’s progress. The entire logic of the code is contained within the try block, and the except block handles conditions where an exception\/error occurs.<\/p>\n Syntax:<\/strong><\/p>\n Output:<\/strong><\/p>\n NameError Exception:<\/strong><\/p>\n In Python, there are various standard exceptions, one of which is NameError. When the identifier being referenced is not declared in the local or global scope, a NameError is thrown. NameError can be caused by a variety of factors, including:<\/p>\n 1. When built-in function is misspelled<\/strong><\/p>\n Output:<\/strong><\/p>\n Here print is misspelled<\/strong><\/p>\n 2. Using variables that aren’t defined<\/strong><\/p>\n Output:<\/strong><\/p>\n Here gvn_string is not defined, we gave gvn_str.<\/p>\n 3. Defining a variable after its use<\/strong><\/p>\n Here we get Name Error since gvn_str is defined after it is used.<\/p>\n 4. Incorrect use of scope.<\/p>\n Output:<\/strong><\/p>\n The following are some of the most common built-in exceptions in Python programming are<\/p>\n AssertionError, FloatingPointError, ImportError, AttributeError, OverflowError, OSError etc<\/p>\n Conclusion<\/strong><\/p>\n You now have an introduction to exceptional handling, and I hope you understand the fundamental concepts of exception handling.<\/p>\n","protected":false},"excerpt":{"rendered":" When we write a program, we can make mistakes that cause errors when we run it. When a Python program meets an unhandled error, it terminates. These errors can be divided into two categories: Syntax Errors Logical Errors(Exceptions) Syntax Errors: Syntax error or parsing error refers to an error caused by not following the right …<\/p>\nnumb = 10\r\nif(numb > 0) # colon(:) is missing here \r\nprint(\"positive number\")\r\nelse:\r\n print(\"Negative number\")\r\n<\/pre>\n
File \"\/home\/ba12f4db06be8a7ce06b7796040d87d0.py\", line 2\r\n if(numb > 0)\r\n ^\r\nSyntaxError: invalid syntax<\/pre>\n
\n
\u00a0Python test Exception Creation<\/h4>\n
gvn_num1 = int(input(\"Enter 1st number = \"))\r\ngvn_num2 = int(input(\"Enter 2nd number = \"))\r\nprint(\"The division of (1st number\/2nd number) = \")\r\nprint(gvn_num1\/gvn_num2)\r\n<\/pre>\n
Enter 1st number = 6\r\nEnter 2nd number = 0\r\nThe division of (1st number\/2nd number) = \r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 4, in <module>\r\nprint(gvn_num1\/gvn_num2)\r\nZeroDivisionError: division by zero<\/pre>\n
Handling Exceptions by try -except Blocks in Python<\/h4>\n
try:\r\n # block of code\r\nexcept <Exception Name>:\r\n # block of code\r\n# Rest of code<\/pre>\n
How to Handle ZeroDivisionError in Python?<\/h4>\n
try:\r\n gvn_num1 = int(input(\"Enter 1st number = \"))\r\n gvn_num2 = int(input(\"Enter 2nd number = \"))\r\n print(gvn_num1\/gvn_num2)\r\nexcept ZeroDivisionError: # exception name for handling it\r\n print(\"The Denominator cannot be zero.Enter a valid number\")\r\n<\/pre>\n
Enter 1st number = 5\r\nEnter 2nd number = 0\r\nThe Denominator cannot be zero.Enter a valid number<\/pre>\n
num = int(input(\"Enter number = \"))\r\nprnt(num)<\/pre>\n
Enter number = 3\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 2, in <module>\r\nprnt(num)\r\nNameError: name 'prnt' is not defined<\/pre>\n
gvn_str = input(\"Enter string = \")\r\nprint(gvn_string)<\/pre>\n
Enter string = hello\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 2, in <module>\r\nprint(gvn_string)\r\nNameError: name 'gvn_string' is not defined<\/pre>\n
print(gvn_str)\r\ngvn_str = input(\"Enter string = \")<\/pre>\n
How to Handle NameError Exception <\/strong>in Python?<\/h4>\n
try:\r\n gvn_str = \"hello\"\r\n print(gvn_string)\r\nexcept NameError:\r\n print(\"NameError Exception. Please check \")\r\n<\/pre>\n
NameError Exception. Please check<\/pre>\n