0% found this document useful (0 votes)
18 views6 pages

Error and Exception

Pkjjjjjnnbnb vvbbbbbbbb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Error and Exception

Pkjjjjjnnbnb vvbbbbbbbb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

error and Exception

error:- Errors are problems in a program that causes the


program to stop its execution.
Errors in Python can be syntax errors
The errors can be broadly classified into two types:

1. Syntax errors
2. Logical errors

3. Syntax error
 The syntax error occurs when we are not following the
proper structure or syntax of the language.
 A syntax error is also known as a parsing error.
 When Python analyse the program and finds an incorrect
statement it is known as a syntax error.
 When the parser (during analyse) found a syntax error it
exits with an error message without running anything.

Common Python Syntax errors:

 Incorrect indentation
 Missing colon, comma, or brackets
 Putting keywords in the wrong place.
EXAMPLE
Logical errors (Exception)

Even if a statement or expression is syntactically correct, the error


that occurs at the runtime is known as a Logical error or
Exception. In other words, Errors detected during execution
are called exceptions.

Common Python Logical errors:

 Indenting a block to the wrong level


 using the wrong variable name
 making a mistake in a boolean expression
Characteristics of Logical Errors
1. No Syntax Error: The code runs successfully without any syntax
errors.
2. Unexpected Output: The program produces output that is different
from what is expected.
3. Difficult to Detect: Logical errors can be subtle and are often harder
to identify and fix compared to syntax errors because the program
appears to run correctly.
4. Varied Causes: They can arise from incorrect assumptions, faulty
logic, improper use of operators, or incorrect sequence of
instructions.

 Exception :-exceptions are raised when some internal


events change the program’s normal flow
 Exceptions are specific types of runtime errors that occur when
the program is running and can disrupt the normal flow of
execution.
 Python raises exceptions when it encounters situations that it
cannot handle during execution (e.g., dividing by zero,
accessing an out-of-range index).
 An exception is a Python object that represents an error..
Python Exception Handling
Python throws errors and exceptions when the code goes wrong, which
may cause the program to stop abruptly. Python also provides an
exception handling method with the help of try-except. Some of the
standard exceptions which are most frequent include IndexError,
ImportError, IOError, ZeroDivisionError, TypeError, and
FileNotFoundError.

. It’s important to handle exceptions properly in your code using try-except


blocks or other error-handling techniques, in order to gracefully handle
errors and prevent the program from crashing.
Try and Except Statement – Catching Exceptions
Try and except statements are used to catch and handle exceptions in
Python. Statements that can raise exceptions are wrapped inside the try
block and the statements that handle the exception are written inside
except block.
Example: Here we are trying to access the array element whose index is
out of bound and handle the corresponding exception.
In the above example, the statements that can cause the error are placed
inside the try statement (second print statement in our case). The second
print statement tries to access the fourth element of the list which is not
there and this throws an exception. This exception is then caught by the
except statement.

Raising Exception
The raise statement allows the programmer to force a specific exception
to occur. The sole argument in raise indicates the exception to be raised.
This must be either an exception instance or an exception class (a class
that derives from Exception).
ORI

f a condition does not meet our criteria but is correct according to the Python interpreter,
we can intentionally raise an exception using the raise keyword. We can use a
customized exception in conjunction with the statement.

EXAMPLE

This code intentionally raises a NameError with the message “Hi


there” using the raise statement within a try block. Then, it catches
the NameError exception, prints “An exception,” and re-raises the same
exception using raise. This demonstrates how exceptions can be raised
and handled in Python, allowing for custom error messages and further
exception propagation.

You might also like