Exception handling is “the process of responding to unwanted or unexpected events when a computer program runs”. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.
Syntax Error:
A syntax error occurs when the code you’ve written violates the rules of the programming language’s syntax. It means that the structure or format of your code is incorrect, making it impossible for the interpreter or compiler to understand and execute it. Syntax errors are usually detected by the compiler or interpreter during the compilation or runtime process and are accompanied by error messages that point to the specific line or area where the error occurred. To fix a syntax error, you need to correct the syntax mistake, such as a missing bracket, incorrect indentation, or misspelled keyword.
Example:
python
<span class="hljs-comment"># Syntax error: Missing closing parenthesis</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">"Hello, World!"</span>
Output:
javascript
<span class="hljs-title class_">SyntaxError</span>: unexpected <span class="hljs-variable constant_">EOF</span> <span class="hljs-keyword">while</span> parsing
I/O Error: An I/O (Input/Output) error typically occurs when there is an issue with reading from or writing to a file, network connection, or any other form of input or output stream. It indicates a problem in the input or output operation, such as a file not found, permission denied, or a disconnected network connection. I/O errors can happen due to various reasons, such as incorrect file paths, insufficient file permissions, hardware failures, or network connectivity issues.
Example:
python
<span class="hljs-comment"># I/O error: File not found</span>
file = <span class="hljs-built_in">open</span>(<span class="hljs-string">"nonexistent_file.txt"</span>, <span class="hljs-string">"r"</span>)
Output:
vbnet
<span class="hljs-symbol">FileNotFoundError:</span> [Errno <span class="hljs-number">2</span>] No such file <span class="hljs-built_in">or</span> directory: <span class="hljs-comment">'nonexistent_file.txt'</span>
Attribute Error:
An attribute error occurs when you try to access or use an attribute or method that doesn’t exist for a particular object. It means that the attribute or method you’re referencing is not defined or available for the object you’re working with. This error commonly happens when you mistype an attribute name, try to access a private attribute, or assume an object has a certain attribute when it doesn’t. Attribute errors are typically raised at runtime and can be resolved by verifying the attribute name, checking the object’s documentation, or making sure you’re using the correct object type.
Example:
python
<span class="hljs-comment"># Attribute error: 'list' object has no attribute 'remov'</span>
my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
my_list.remov(<span class="hljs-number">2</span>)
Output:
csharp
AttributeError: <span class="hljs-string">'list'</span> <span class="hljs-built_in">object</span> has no attribute <span class="hljs-string">'remov'</span>
Remember to review the error messages and the line numbers mentioned in the errors to locate and fix the specific issues causing the errors.
