Skip to main content

Introduction

In Java, an exception is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions. When an exception occurs, the program stops executing the current code block and jumps to a special block of code called an exception handler. Exceptions are used to handle various error conditions or exceptional situations that may occur during program execution. 

                                                            OR

An expected unwanted event that disturbs the normal flow of the program is called an exception.
eg-:
      Sleeping Expection.
      Suddenly rain start.
      File not found exception.




It is highly recommanded to handle exception and the main objective of exception is graceful termination of the program.
Exception handling does not mean repairing and exception, we have to provide alternative way to continue rest of the program is the concept of exception handling.

For example, our programming requirement is to read data from remote file locating at Landon at run time if Landon file is not available our program should not be terminated abnormally. We have to provide some local file to continue the rest of the program, normally this way of defining an alternative is nothing but exception handling. 

eg-:

try{
     read data from remote file location at Landon

}catch(FileNotFoundException e){
    use local file & continue rest of the program normally
}

Explanation of above the code.
The code you provided appears to be a simplified example of error handling in Java using a try-catch block. Let's break it down step by step:

1. **`try` Block:**
   

   try {
       // read data from remote file location at Landon
   }
 
   This is the `try` block, where you place the code that might throw an exception. In this case, it's trying to read data from a remote file location at Landon.

2. **`catch` Block:**
   
   
   catch (FileNotFoundException e) {
       // use local file & continue rest of the program normally
   }
   
   This is the `catch` block, which is used to catch and handle exceptions. In this case, it's specifically catching the `FileNotFoundException` exception. If an exception of this type is thrown in the `try` block, the code inside the `catch` block will be executed.

3. **Handling `FileNotFoundException`:**
   
   If the code inside the `try` block encounters a `FileNotFoundException`, it means that the remote file at the specified location in Landon does not exist or cannot be accessed. In such a case:

   - The code inside the `catch` block will be executed.
   - The comment in the `catch` block suggests that the program will use a local file instead. This is a common strategy when working with remote resources. If the remote resource is unavailable, you can fall back to a local resource or a default behaviour.
   - After handling the exception, the program will continue executing the rest of the code normally, as if the exception never occurred.



Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...