Exception Handling in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay Bhalala
    New Member
    • Nov 2014
    • 119

    Exception Handling in Java

    Hello,

    I have create the program for exception handling using nested try in java

    Here is my program :

    Code:
    class nestedtry
    {
    	public static void main(String args[])
    	{
    		try
    		{
    			int a,b;
    			a=5;
    			b=0;
    			try
    			{
    				int a[];
    				a=new int[3];
    				for(int i=0;i<=4;i++)
    				{
    					a[i]=10;
    				}
    			}
    			catch(ArrayIndexOutOfBoundsException e)
    			{
    				System.out.print("Array " + e);
    			}
    		}
    		catch(ArithmeticException e)
    		{
    			System.out.print(e);
    		}
    		System.out.print("From main");
    	}
    }
    There is error occurred in this program...

    Error :
    Code:
    23.java:12: a is already defined in main(java.lang.String[])
                                    int a[];
                                        ^
    1 error
    What will be the correction in my program for solve the error????
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Look at line 7 and 12. You have defined a simple int "a" and an int array "a[]". You cannot give the same name here. The problem is if the compiler would allow it, then it cannot read your mind which of the both to print if you typed
    Code:
    "System.out.println("int or array: value=" + a);"
    The variable int a is also visible in the inner block (your inner try block), but it is not visible outside of its own block (your outer try block)

    Comment

    Working...