473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Exception Handling in Java

Ajay Bhalala
119 64KB
Hello,

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

Here is my program :

Expand|Select|Wrap|Line Numbers
  1. class nestedtry
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         try
  6.         {
  7.             int a,b;
  8.             a=5;
  9.             b=0;
  10.             try
  11.             {
  12.                 int a[];
  13.                 a=new int[3];
  14.                 for(int i=0;i<=4;i++)
  15.                 {
  16.                     a[i]=10;
  17.                 }
  18.             }
  19.             catch(ArrayIndexOutOfBoundsException e)
  20.             {
  21.                 System.out.print("Array " + e);
  22.             }
  23.         }
  24.         catch(ArithmeticException e)
  25.         {
  26.             System.out.print(e);
  27.         }
  28.         System.out.print("From main");
  29.     }
  30. }
  31.  
There is error occurred in this program...

Error :
Expand|Select|Wrap|Line Numbers
  1. 23.java:12: a is already defined in main(java.lang.String[])
  2.                                 int a[];
  3.                                     ^
  4. 1 error
  5.  
What will be the correction in my program for solve the error????
Oct 8 '15 #1
1 1126
chaarmann
785 Expert 512MB
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
Expand|Select|Wrap|Line Numbers
  1. "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)
Oct 9 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Ekim | last post by:
hy, I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
13
by: tolisss | last post by:
Hi i have setup a global exception handler b4 Application.Run like Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException ); then after...
13
by: junw2000 | last post by:
Is C++ Exception handling useful? think it is too complicated. What kinds of project need to use it? Thanks.
2
by: ranadhirnag | last post by:
We test a particular constriant in our XSLT and throw a custom exception on failure: <xsl:when test="......"> <xsl:value-of select="exception:throwException('Recording failed')"/> </xsl:when> ...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
7
by: =?Utf-8?B?THVib21pcg==?= | last post by:
Hi, Is it possible to set up C# compiler so that I would get a warning when I miss any exception thrown by a framework? Something like in Java, where the compiler forces you to handle all...
4
by: DHS1 | last post by:
Hey guys. I have a lab that is due in two weeks, but I wanted to start on it now. Problem is, I'm at home during christmas break so I can't ask my professors. Here's my problem: I am given a very...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.