Exception Handling in Python | Python Exception Handling
Your Queries: exception handling in python, python exception handling, try except python, exceptions in python, It is possible to write programs that handle selected exceptions.
The cause of an exception is often external to the program itself. For example, an incorrect input, a malfunctioning IO device etc. Because the program abruptly terminates on encountering an exception, it may cause damage to system resources, such as files. Hence, the exceptions should be properly handled so that an abrupt termination of the program is prevented.
Until now error messages haven’t been more than mentioned, but if you have tried out the examples you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions.
1. Syntax Errors
Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python:
while True print('Hello world')
Error:
while True print('Hello world')
^
SyntaxError: invalid syntax
the error is detected at the function print(), since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script.
2. Exceptions
Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally error: you will soon learn how to handle them in Python programs.
#a = 10/0
#1 + myvar + 1
v1 = '2' + 2
The last line of the error message indicates what happened. Exceptions come in different types, and the type is printed as part of the message: the types in the example are ZeroDivisionError, NameError and TypeError. The string printed as the exception type is the name of the built-in exception that occurred. This is true for all built-in exceptions, but need not be true for user-defined exceptions (although it is a useful convention). Standard exception names are built-in identifiers (not reserved keywords).
3. Handling Exceptions
It is possible to write programs that handle selected exceptions. Look at the following example, which asks the user for input until a valid integer has been entered, but allows the user to interrupt the program (using Control-C or whatever the operating system supports)
def int_input(prompt):
while True:
try:
age = int(input(prompt))
return age
except ValueError as e:
print("Not a proper integer! Try it again")
#pythontutorial
#pythoncourse
#learnpython
#pythonprojects
#pythonprogramming
#pythoncrashcourse
#pythonbasics
#pythonforbeginners
#pyhon
#pythonbeginner #exceptionhandling #exceptions
#exceptionhandlinginpython
#errorhandling
#syntaxerror
Python Multiple Exception Handling
Python Developer
Python Language
Python Programming
Exception Handling In Python Example
Python For Beginners
Channel: https://www.youtube.com/@NewsOnCoding
? - FACEBOOK: https://www.facebook.com/profile.php?id=100094506555328
Видео Exception Handling in Python | Python Exception Handling автора PHP: Углубленное изучение
Видео Exception Handling in Python | Python Exception Handling автора PHP: Углубленное изучение
Информация
1 декабря 2023 г. 4:38:15
00:18:40
Похожие видео