-2

I am new to exception handling in java. I was just trying to clear my concepts,but i encountered the following problem.

Here is the code.

class Inn {
    public static void main(String... args) {
        try{
            String i="Asd";
        } catch(Exception e) {
            System.out.println(e);
        }
        //i=7;
        System.out.println(i);
    }
}

And here is the error which is coming.

G:\javap>javac Inn.java
Inn.java:13: error: cannot find symbol
                System.out.println(i);
                                   ^
  symbol:   variable i
  location: class Inn
1 error
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
asad
  • 7
  • 5

6 Answers6

2
try { // Block A
    String i="Asd"; 
} // End of Block A
catch(Exception e) {
    System.out.println(e);
}

System.out.println(i); // 'i' does not exist in this scope

The variable i is declared within the code block A, which means that it could only be accessible from inside of it (notice the { curly braces } that limit its scope). As soon as the execution flow passes by the block A, the variable i will no longer exist in scope.

That being said, if you want to make this work, you gonna have to declare the string i out of the inner scope:

String i = ""; // necessary initialization, without it you'll get a
               // "variable may have not been initialized" error
try { 
    i = "Asd"; 
}
catch(Exception e) {
    System.out.println(e);
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
1

The i variable is defined in a different scope and is not visible at the point where you are trying to print it.

Rewrite is as:

import java.io.*;
class Inn
{
    public static void main(String s[])
    {
        String i = null;
        try{
            i="Asd";
        }catch(Exception e)
        {
            System.out.println(e);
        }
        //i=7;
        System.out.println(i);
    }
}

That way, variable i is in the same scope as the println statement.

TT.
  • 15,774
  • 6
  • 47
  • 88
1

With most programming languages, one generally associates a scope to a variable. What the scope means in layman terms is - the section of code where the variable can be used or where the variable is visible.

With Java (or many OOP languages for that matter) we generally have various levels of scope :

  1. Class Level Scope : Variables defined in the class i.e the member variables can be accessed anywhere within the class (I'm keeping the static modifier out of the picture for the sake of simplicity). These variables are generally defined at the top of the class (convention). Keep in mind that these variables need to be outside the methods of the class to be in the class level scope.

    public class Example {
        private String a; // variable a can be accessed anywhere inside the class
    }
    
  2. Method Level Scope : Variables defined within a method can be accessed inside the method only. Their lifetime ends when the method returns.

     private int giveMeFive(){
        int a = 5; // Scope of a is within the method
        return a; // When a is returned then there the variable a dies :(
    }
    
  3. Loop Level Scope : Variables you define within the loops are restricted to that loop and any inner loops. They cannot be accessed outside the loop in which they are defined.

     public static void main(String []args){
        for(int i=0;i<10;i++){
            System.out.println(i); // Only i is accessible here but not j
            for(int j=1;j<5;j++){
                System.out.println(i+j); // Both i and j are accessible here
            }
        }
     }
    
  4. Block level scope : In general, whatever resides inside of curly brackets { } defines a particular scope. In Java you can usually access a variable as long as it was defined within the same set of brackets or any brackets within these brackets.

In your case you defined the variable i within the try block so as soon as the try block finished the variable was no longer visible and hence could not be found by your println statement later on in the code.

     public static void main(String[] args){
         String i; // i defined outside the try block so it can be accessed after the try finished running
         try{
             i= "Asd"
         }catch(Exception e){
             Sytem.out.println(e);
         }
         System.out.println(i);
     }

All the best :)

Narayan Acharya
  • 1,459
  • 1
  • 18
  • 33
0

It is because the scope of i is juste in try block.

Damien Chesneau
  • 457
  • 2
  • 18
0

Variable i is defined inside the try block, which restricts the scope of i inside try block. You need to define it outside the try block.

noorul
  • 1,283
  • 1
  • 8
  • 18
0

Java is an scoped language, therefore the visivility of a variable depends of where it is defined.

In computer programming, the scope of a name binding – an association of a name to an entity, such as a variable – is the part of a computer program where the binding is valid: where the name can be used to refer to the entity. Java is lexically scoped.

Problem with "scopes" of variables in try catch blocks in Java

Because your string i was declared in the try block, the println method is not able to find it. (it is out of scope)

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97