0

Possible Duplicate:
exception handeling, creating log and continue the program in JAVA

I am designing a program in JAVA that captures results in about 10 iterations, and after that i have to write all the results in a log file, I need a solution that if any exception occurs then it should be written on my text file and secondly the program must not stop, it must go on till the last iteration is completed... ie if some error occur on any part of any iteration, it must be mentioned within my results by the name of error and the program must not stop here, it must go on and update my log file

see this just for example: because my class is around 1800 lines can't paste all here.

float j,i,t;
int []a={1,2,0,3,4,5};
int []b={6,6,9,3,4,5};
for(int i=0;i<6;i++)
{
   j=2/a[i];
   i=3/b[i];
   t=4/b[i];

 }

so here due to 0 in list of values of arrray an exception occurs, thus stopping the whole iteration, i want that to skip, if some exception occur the loop must not be broken, it just leave out that exception part and write on a text file.

Cœur
  • 37,241
  • 25
  • 195
  • 267
rahul
  • 205
  • 3
  • 6
  • 12
  • Please, don't post multiple similar questions. Just edit the original: http://stackoverflow.com/questions/10546504/exception-handeling-creating-log-and-continue-the-program-in-java – yatul May 11 '12 at 08:18
  • sry, am new to this, i was trying to delete the previous one but failed – rahul May 11 '12 at 08:19

2 Answers2

1

try this:

float j;
int []a={1,2,0,3,4,5};
for(int i=0;i<6;i++)
{
    try{
        j=2/a[i];
    catch(Exception e){ //a more specific exception would be preferable, but I don't know what other exceptions may occur here
        System.out.println("Exception occurred at iteration: "+i);
    }
}

Edit: just realised, having the value in the print statement might also cause an exception if the array is too small.

Second Edit: Exception handling isn't as efficient as simple checks i.e. if(a[i] == 0), so using checks would be a more efficient way of going about this.

Perry Monschau
  • 883
  • 8
  • 22
  • You put this inside the loop. just around the code that is shown. – Perry Monschau May 11 '12 at 08:23
  • sry, i have to edit part, i have done a big mistake in describing que, plz see the edit part again... – rahul May 11 '12 at 08:24
  • now see, i have to get all the results, exception could occour at any part, but i want all the result..i want my prog to act smart and leave the exception out...without affecting other values – rahul May 11 '12 at 08:29
  • its just an example, my code is about much stronger and unpredictable exceptions, its java.net based – rahul May 11 '12 at 08:31
  • Ok, if there would be lots of exceptions, use simple checks. I also think you should learn more about exception handling as the other answerer states. If your code is too messy for you to figure out where exceptions might be, then it's probably time to clean it up a bit. – Perry Monschau May 11 '12 at 08:31
0

I think you should read the answers to this question to learn more about exception handling.

Community
  • 1
  • 1
aniri
  • 1,831
  • 1
  • 18
  • 28