<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged exceptions</title>
<link>https://coderlegion.com/tag/exceptions</link>
<description>Powered by Question2Answer</description>
<item>
<title>Handling Exceptions Like a Pro in Java</title>
<link>https://coderlegion.com/4210/handling-exceptions-like-a-pro-in-java</link>
<description>&lt;h3 id=&quot;handling-exceptions-like-a-pro-in-java-stop-using-catch-all-blocks&quot;&gt;Handling Exceptions Like a Pro in Java: Stop Using catch-All Blocks&lt;/h3&gt;
&lt;p&gt;If you only handle &lt;em&gt;Exceptions&lt;/em&gt; in Java with just generic &lt;code&gt;try-catch&lt;/code&gt; then I appeal you to stop and try these below tips instead, you'll thank me later :)&lt;/p&gt;
&lt;p&gt;Let’s be honest — most of us start out doing the lazy thing:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;try {
    // risky code
} catch (Exception e) {
    e.printStackTrace();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Looks safe. But what you've actually written is the software equivalent of sweeping all the dirt under the rug. No clue what went wrong. No proper recovery. Just vibes. Which I bet is a menace to mankind.&lt;/p&gt;
&lt;p&gt;Let’s fix that. Let's learn how real Java pros handle exceptions — with intention, clarity, and sometimes, a touch of functional programming.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;the-problem-with-catch-alls&quot;&gt;The Problem with Catch-Alls&lt;/h4&gt;
&lt;p&gt;Catching &lt;em&gt;Exception&lt;/em&gt; or worse, &lt;em&gt;Throwable&lt;/em&gt;, is like saying “I don’t know what might break, so I’ll just catch everything.” But that makes your app unpredictable and hard to debug.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;catch (Exception e) {
    // What are you even catching here? FileNotFound? NullPointer?
    // Did you miss an InterruptedException that should be re-thrown?
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You're hiding the bugs that should scream in your face during development.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;So rule no. 1:&lt;/strong&gt; Be specific. Catch only what you’re ready to handle.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;catch-the-exact-thing-you-re-solving&quot;&gt;Catch the Exact Thing You’re Solving&lt;/h4&gt;
&lt;p&gt;Let's say you're reading a file:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;try {
    Files.readAllLines(Paths.get(&quot;data.txt&quot;));
} catch (IOException e) {
    System.err.println(&quot;Could not read the file: &quot; + e.getMessage());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Much better! Now your intention is clear, and you're handling something specific.&lt;/p&gt;
&lt;p&gt;Bonus: This also helps you avoid accidentally swallowing critical runtime exceptions like &lt;code&gt;OutOfMemoryError&lt;/code&gt; or &lt;code&gt;InterruptedException&lt;/code&gt; — things that you should not handle casually.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;move-to-functional-style-exception-handling&quot;&gt;Move to Functional-Style Exception Handling&lt;/h4&gt;
&lt;p&gt;Functional programming has been slowly sipping tea and watching us drown in imperative chaos. But now it’s time to borrow some of its elegance.&lt;/p&gt;
&lt;p&gt;Enter libraries like &lt;strong&gt;&lt;em&gt;Vavr&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;With &lt;em&gt;Vavr&lt;/em&gt;, you wrap potentially failing logic into a Try, and then compose your actions in a clear, fluent way which is more readable and soothing to eyes &lt;/p&gt;
&lt;p&gt;Example :&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;import io.vavr.control.Try;

Try&amp;lt;String&amp;gt; content = Try.of(() -&amp;gt; new String(Files.readAllBytes(Paths.get(&quot;data.txt&quot;))))
    .onFailure(e -&amp;gt; System.err.println(&quot;Something went wrong: &quot; + e.getMessage()))
    .map(String::toUpperCase);

content.forEach(System.out::println);

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look at that! No try-catch noise, just pure, functional clarity. You’ve isolated the risky part and handled failure explicitly, without polluting your logic.&lt;/p&gt;
&lt;p&gt;You can even chain retries, fallbacks, and defaults like a boss:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;String result = Try.of(() -&amp;gt; riskyCall())
    .recover(throwable -&amp;gt; &quot;default&quot;)
    .get();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is &lt;strong&gt;&lt;em&gt;real&lt;/em&gt;&lt;/strong&gt; control over flow, not just catching and logging.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;checked-vs-unchecked-exceptions-handle-like-you-mean-it&quot;&gt;Checked vs Unchecked Exceptions — Handle Like You Mean It&lt;/h4&gt;
&lt;p&gt;Java makes this distinction. Checked exceptions (like IOException, SQLException) force you to handle or declare them. Unchecked exceptions (like &lt;code&gt;NullPointerException&lt;/code&gt;, &lt;code&gt;IllegalArgumentException&lt;/code&gt;) are on you to prevent.&lt;/p&gt;
&lt;p&gt;Here’s the mindset shift:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Checked&lt;/strong&gt;: These are expected problems (e.g., file not found, DB connection fails). Handle or bubble up with context.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unchecked:&lt;/strong&gt; These are code smells and should be avoided upfront via validation and logic.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Don’t convert checked exceptions into unchecked ones just because it's easier. That’s like turning off the fire alarm because it’s loud.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;-clean-exception-strategy&quot;&gt;♂️ Clean Exception Strategy&lt;/h4&gt;
&lt;p&gt;Here’s what pros usually follow:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Fail fast.&lt;/strong&gt; Don’t catch what you can’t handle meaningfully.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Wrap with context&lt;/strong&gt;. If rethrowing, always add context.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use custom exceptions&lt;/strong&gt; to model your domain (e.g., &lt;code&gt;UserNotFoundException&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Don’t log and throw.&lt;/strong&gt; Do one — logging is for terminal exceptions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Don’t just suppress&lt;/strong&gt;. Silent catch blocks are crime scenes.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h4 id=&quot;-bonus-tools-that-help&quot;&gt;️ Bonus Tools That Help&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Lombok’s &lt;code&gt;@SneakyThrows&lt;/code&gt;: Can be useful, but also dangerous — it hides checked exceptions.&lt;/li&gt;
&lt;li&gt;Vavr’s &lt;code&gt;Try&lt;/code&gt;, &lt;code&gt;Either&lt;/code&gt;, &lt;code&gt;Option&lt;/code&gt;: Bring Scala-style control flow to Java, and let you reason about success/failure better.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ExceptionUtils&lt;/strong&gt; from Apache Commons: Helps in deep exception analysis.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h4 id=&quot;final-thoughts&quot;&gt;Final thoughts&lt;/h4&gt;
&lt;p&gt;If you're still throwing catch-all blocks and logging stack traces like confetti, it’s time to upgrade. Java gives you the tools to be precise, and functional programming gives you the grace.&lt;/p&gt;
&lt;p&gt;Your goal should not be to handle exceptions. It should be to own them.&lt;/p&gt;
&lt;p&gt;Make your exception handling tell a story — one that debuggers, teammates, and even future-you will appreciate.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Stop catching Exceptions, start catching intent.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
<category>API with Java</category>
<guid isPermaLink="true">https://coderlegion.com/4210/handling-exceptions-like-a-pro-in-java</guid>
<pubDate>Mon, 21 Jul 2025 19:21:30 +0000</pubDate>
</item>
<item>
<title>Python Exception Handling</title>
<link>https://coderlegion.com/166/python-exception-handling</link>
<description>&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; While working on the Python code you come across a situation where your &lt;strong&gt;program is stopped by the compiler&lt;/strong&gt; during the execution and something like an error is displayed on the terminal, &lt;strong&gt;exception handling was the solution&lt;/strong&gt; at that time. Well, that is a common problem in every developer's life. In this tutorial I will talk about Python exceptions and different types of exceptions with examples, also why exception handling is important, and how Python manages these exceptions effectively. Let's get started.&lt;/p&gt; 
&lt;h2 style=&quot;margin: 0.5;&quot;&gt; Why Error Handling Matters?&lt;/h2&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; Suppose you are using a food ordering application and suddenly the app crashes. You have no idea why the app stopped working. So, exception handling is essential as it &lt;strong&gt;prevents the app from future errors&lt;/strong&gt; and handles them effectively. Exception handling helps you with the smooth execution of your programs and &lt;strong&gt;removes the risk of abnormal event occurrence&lt;/strong&gt; if it's handled during the program development phase. If some errors are left unchecked then that will cause the application to crash, data loss, and worse, so that is the reason exception handling is a significant matter.&lt;/p&gt;
&lt;ol&gt;
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt;It &lt;strong&gt;prevents&lt;/strong&gt; the software from &lt;strong&gt;crashing and stopping abruptly&lt;/strong&gt;.&lt;/li&gt;
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt;Make the &lt;strong&gt;user experience better&lt;/strong&gt; by reducing the frustration and confusion with properly displaying the error message.&lt;/li&gt;
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt;It &lt;strong&gt;ensures data safety and integrity&lt;/strong&gt; and handles data reliability.&lt;/li&gt;
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt;It also helps the developer to &lt;strong&gt;safeguard&lt;/strong&gt; their application from &lt;strong&gt;hacker attacks&lt;/strong&gt;.&lt;/li&gt;
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt;It &lt;strong&gt;reduces&lt;/strong&gt; the need for future &lt;strong&gt;cost increments for technical support&lt;/strong&gt; and saves time beforehand.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 style=&quot;margin: 0.5;&quot;&gt; How Python Manages Errors with Exception Handling?&lt;/h2&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; During your programming life, you may encounter many types of errors, which can be a &lt;em&gt;syntactical error, an error that comes upon run-time, or a logical error.&lt;/em&gt; We can resolve the &lt;strong&gt;syntax error&lt;/strong&gt; simply because it will show the &lt;strong&gt;error on the compilation time &lt;/strong&gt;run time error  occurs it will not halt the program compilation but &lt;strong&gt;shows unexpected behavior&lt;/strong&gt; during the execution time that &lt;em&gt;deviates from the normal flow of the program&lt;/em&gt;. &lt;/p&gt;
&lt;div class=&quot;div-blue&quot;&gt; 
&lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt; 
&lt;span class=&quot;alert-body&quot;&gt;Remember, a syntax error is a common error that can be caught before execution but if it is not caught in some situation at compile time then there is one built-in exception as well.&lt;/span&gt;
&lt;/div&gt; 
&lt;h3 style=&quot;margin: 0.5;&quot;&gt; Exception Handling Different Ways&lt;/h3&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; Python manages the errors by &lt;strong&gt;writing the program&lt;/strong&gt; that handles the exception. There are many &lt;strong&gt;ways&lt;/strong&gt; for exception handling in Python. So, let's dive into it:&lt;/p&gt; 
&lt;ul&gt;   

&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt; &lt;strong&gt;Try:&lt;/strong&gt; When you see that a &lt;strong&gt;specific piece of code&lt;/strong&gt; will cause the error, you &lt;strong&gt;put it inside&lt;/strong&gt; the &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/164/python-try-except-blocks&quot; target=&quot;_blank&quot;&gt;try&lt;/a&gt; block.&lt;/li&gt;   

&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt; &lt;strong&gt;Except:&lt;/strong&gt; If the code in the try block creates an error. Python will &lt;strong&gt;jump to the except block&lt;/strong&gt; and execute it. So, right after the try block, we must write the except block. &lt;/li&gt; 

&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt; &lt;strong&gt;Finally:&lt;/strong&gt; After using the try and except block, you can use the &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/185/python-finally-block&quot; target=&quot;_blank&quot;&gt;finally&lt;/a&gt; block which always &lt;strong&gt;executes whether the exception occurs or not&lt;/strong&gt;.&lt;/li&gt;  

&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt; &lt;strong&gt;Else:&lt;/strong&gt; Instead of using the finally block, you can also use the else block right after a try-and-except block. It will help you to make sure that &lt;strong&gt;no exception occurs&lt;/strong&gt; in the program, as it will only run when there is no exception.&lt;/li&gt; 

&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt; &lt;strong&gt;Raise:&lt;/strong&gt; One more exception handling is done by using the &lt;strong&gt;raise keyword&lt;/strong&gt;, you can use it without using try and except block and just raise an exception that would most probably pop up.&lt;/li&gt; 
&lt;/ul&gt; 
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;It is important to use except block when you are using a try block. Many except blocks can be used with one try block&lt;/span&gt;
&lt;/div&gt;
&lt;h2 style=&quot;margin: 0.5;&quot;&gt; Understanding Exceptions&lt;/h2&gt; 
&lt;h3 style=&quot;margin: 0.5;&quot;&gt; Exceptions in Python&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; Exception is like a situation that halts the normal flow of the program and stops the program in between without any reason. Exceptions are not related to the syntax error in the program but a run time issue. It will help the &lt;strong&gt;program to run smoothly and improve the robustness&lt;/strong&gt; of the program. In Python, an exception is like a &lt;strong&gt;capsule that encapsulates the error events&lt;/strong&gt; related to the program's abnormal behavior. When the Python compiler encounters a situation that cannot be handled on its own, it raises the exception by &lt;strong&gt;signaling the developer to resolve it&lt;/strong&gt;.&lt;/p&gt; 
&lt;h2 style=&quot;margin: 0.5;&quot;&gt; Exploring Different Types of Exceptions&lt;/h2&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; As we know there are &lt;strong&gt;two&lt;/strong&gt; types of exceptions in Python, each has its &lt;strong&gt;use case&lt;/strong&gt; and can be used by the developer in certain situations. These are:&lt;/p&gt; 
&lt;ul&gt; 
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt;&lt;strong&gt; Built-in Exception: &lt;/strong&gt; A built-in exception is a type that is &lt;strong&gt;already defined&lt;/strong&gt; in Python. These exceptions normally encounter errors by many of you, so Python developed the module based on it, which is helpful and time-saving.&lt;/li&gt;  
&lt;li style=&quot;text-align: justify; margin: 1;&quot;&gt; &lt;strong&gt;User-defined exception &lt;/strong&gt; Sometimes a situation occurs where you cannot handle it by the available built-in exceptions, therefore Python allows you to make the &lt;strong&gt;custom exception&lt;/strong&gt; to handle this type of problem and the programmer has to define the exception on its own with coding.&lt;/li&gt; 
&lt;/ul&gt; 
&lt;h2 style=&quot;margin: 0.5;&quot;&gt; Examples of Common built-in Exceptions&lt;/h2&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; Python has a &lt;strong&gt;vast range of built-in exceptions&lt;/strong&gt;. Each exception has been used for specific error occurrences. Each has its use case. So, let's explore each one of them:&lt;/p&gt; 
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;1: Value Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception occurs when the function gets an &lt;strong&gt;argument&lt;/strong&gt; with the &lt;strong&gt;correct datatype&lt;/strong&gt;, but the value of that &lt;strong&gt;datatype is invalid&lt;/strong&gt;.&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#here we pass the string to int datatype that is an invalid value 
number = int(&quot;string&quot;) 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=17883826825220453929&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;2: Type Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception occurs when the &lt;strong&gt;operation&lt;/strong&gt; is performed on an &lt;strong&gt;incorrect type.&lt;/strong&gt;&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#here we perform an addition operation on a string with an int datatype that is inappropriate
number = 7 + &quot;7&quot; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=11768622382153601869&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;3: Assert Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception occurs when the &lt;strong&gt;assert expression fails&lt;/strong&gt;. Its main purpose is to debug and check expressions.&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#here assertion error occurs because the expression fails 
assert 5!=5
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=9188483604105506784&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;4: Module Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception occurs when you are trying to &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/217/mastering-importing-modules-in-python&quot; target=&quot;_blank&quot;&gt;import the module&lt;/a&gt;, but Python &lt;strong&gt;cannot locate the module&lt;/strong&gt;.&lt;/p&gt;  
&lt;pre&gt;&lt;code&gt;#here import cannot locate the specified module 
import module_Name 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=7206665820801744251&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;5: Index Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; This exception occurs when you try to &lt;strong&gt;access the number&lt;/strong&gt; from the list that is not added or &lt;strong&gt;out of range&lt;/strong&gt;.&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#here num_add variable tries to access the number at index 6 which is out of range 
number_list = [1,2,3,4,5] 
num_add = number_list[6] 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=3474679075458745563&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;6: ZeroDivision Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception normally occurs when you try to &lt;strong&gt;divide a number&lt;/strong&gt; with the second operand as &lt;strong&gt;zero&lt;/strong&gt;.&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#here num variable stores the result by dividing the 10 by 0 
num  = 10/0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=1039050150387168991&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;7: Name Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; This exception occurs when you try to use a &lt;strong&gt;variable&lt;/strong&gt; that is &lt;strong&gt;not defined locally and globally&lt;/strong&gt; in the program&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#here when the variable is undefined, and you are trying to use it 
num = 10 
num2 = num/num3 
print(num2) 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=14814752257778344785&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;8: Syntax Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception occurs when the &lt;strong&gt;incorrect syntax is not detected&lt;/strong&gt; by the parse or interpreter before the code compilation.&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#here syntax is incorrect 
num  10; 
num2 50; 
result 10 + 50
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=4449602819913702930&quot; alt=&quot;&quot;&gt; &lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;9: FileNotFound Error&lt;/h3&gt; 
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception occurs when you &lt;strong&gt;try to open the file but it fails&lt;/strong&gt; because the compiler cannot find the specific file in your program folder. This exception is the &lt;strong&gt;subclass of the OSError exception.&lt;/strong&gt;&lt;/p&gt; 
&lt;pre&gt;&lt;code&gt;#Try to manipulate the file that is not available and cannot be found at the provided path 
def read_content_from_file(file_path):
    with open(file_path, 'r') as file:
        content =  file.readlines()
    
file_path = &quot;file.txt&quot;
read_content_from_file(file_path)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=5856662375552249816&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 style=&quot;margin: 0.5;&quot;&gt;10: Indentation Error&lt;/h3&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt;This exception usually occurs when you &lt;strong&gt;use incorrect indentation&lt;/strong&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#IndentationError: incorrect indent/space is used at the return line 
def add(): 
    num1 = 7 
    num2 = 25 
    result = num1 + num2 
        return result
    
cal = add()
print(&quot;Sum of two numbers&quot;, cal)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=1198014179764549574&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-blue&quot;&gt; 
&lt;span class=&quot;alert-header&quot;&gt;Note:&lt;/span&gt; 
&lt;span class=&quot;alert-body&quot;&gt;All exception types are objects because they are inherited from the exception base class.&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;div-faq&quot; style=&quot;margin-bottom: 15px; margin-top: 3px;&quot;&gt; 
&lt;span class=&quot;faq-header&quot; style=&quot;font-weight: bold; color: dark-gray;&quot;&gt;FAQs&lt;/span&gt; 
&lt;span class=&quot;faq-body&quot; style=&quot;margin-top: 3px; margin-bottom: 20px;&quot;&gt; Q: Can I create my exception? &lt;br&gt;A: Yes, you can define your custom-built exception class by inheriting it from the Exception base class.&lt;/span&gt; 
&lt;span class=&quot;faq-body&quot; style=&quot;margin-top: 3px; margin-bottom: 20px;&quot;&gt;Q: Is error or exception handling different? &lt;br&gt; A: Yes, there are many types of error that you can catch before compiling the code but sometimes the error occurs where only exception handling is required.&lt;/span&gt; 
&lt;span class=&quot;faq-body&quot; style=&quot;margin-top: 3px;&quot;&gt; Q: Is it possible to execute the code regardless of checking error occurrence? &lt;br&gt; A: Yes, by using the finally block with the try and catch block you can execute the code that is written in the finally block because the finally block will execute whether the exception occurs or not.&lt;/span&gt;
&lt;/div&gt;
&lt;h2 style=&quot;margin: 0.5;&quot;&gt;Wrapping Up&lt;/h2&gt;
&lt;p style=&quot;text-align: justify; margin: 1;&quot;&gt; In conclusion, we discussed the need for exception handling in the software development process before the &lt;strong&gt;program is released&lt;/strong&gt; in the market or available for the user. As you know, exception handling is a &lt;strong&gt;powerful concept of programming&lt;/strong&gt; language. It helps you as well as &lt;strong&gt;prepares you beforehand for future errors&lt;/strong&gt;. Many &lt;strong&gt;built-in exceptions&lt;/strong&gt; provided by Python cover most of your errors during the program development but you can also use the &lt;strong&gt;custom-based exception&lt;/strong&gt; if you encounter a situation where no built-in exception is helpful, this problem is also resolved by the Python programming language. Now, I hope you will easily handle the exception in your code. Thank you for reading the guide. Happy Coding!&lt;/p&gt;
&lt;hr&gt; 
&lt;h2 style=&quot;margin: 0.5;&quot;&gt; Reference&lt;/h2&gt; 
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/tutorial/errors.html&quot; title=&quot;Exceptions&quot;&gt; Errors and Exceptions Handling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://python101.pythonlibrary.org/chapter7_exception_handling.html&quot; title=&quot;Exception handling&quot;&gt;Exception Handling in Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Error Handling</category>
<guid isPermaLink="true">https://coderlegion.com/166/python-exception-handling</guid>
<pubDate>Mon, 04 Mar 2024 22:54:04 +0000</pubDate>
</item>
</channel>
</rss>