Solved

Exit loop placed in Try-Catch using Break and Continue

Posted on 2003-10-30
15
605 Views
Last Modified: 2010-03-31
Hi,
My code have two sections that I commented it.
I want to using only break and contine or ... keywords
and labels, change behavor (output) of section 1 as like
as section 2 and vise versa.

Thank you for your comments
-FA


// CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE CODE

public class TestClass {
      public static void main(String args[]) {
            int[][] intArray = new int[4][];
            intArray[0] = new int[1];
            intArray[1] = new int[3];
            intArray[2] = new int[2];
            intArray[3] = new int[3];

            intArray[0][0] = 12;
            intArray[1][2] = 16;
            intArray[2][1] = 17;
            intArray[3][2] = 15;

            // Section 1
            System.out.println("In Section 1");
            for (int i = 0; i <= 3; i++) {
                  for (int j = 0; j<=2; j++) {
                        try {
                              System.out.println(intArray[i][j]);
                        } catch (Exception e) {
                              System.out.println("N/A");
                        }
                  }
            }

            // Section 2
            System.out.println("\nIn Section 2");
            try {
                  for (int i = 0; i <= 3; i++)
                        for (int j = 0; j<=2; j++)
                              System.out.println(intArray[i][j]);
            } catch (Exception e) {
                  System.out.println("N/A");
            }

      }
}


// CODE ENDED  CODE ENDED  CODE ENDED  CODE ENDED  CODE ENDED
0
Comment
Question by:Farzad Akbarnejad
  • 6
  • 6
  • 2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9652121
Sounds like homework...
0
 
LVL 14

Author Comment

by:Farzad Akbarnejad
ID: 9652273
CEHJ,
No it isn't homework. I programmed C++ and Visual Basic for some years and I start Java for two weeks.
I like two dimentional arrays.
I intersted in Exceptions that I didn't use it in C++.
I wrote this example myself to knows about Exceptions more.

Thanks
-FA


0
 
LVL 35

Expert Comment

by:TimYates
ID: 9653036
I don't know what you mean...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 35

Expert Comment

by:TimYates
ID: 9672826
Fair enough by me :-)

> I don't know what you mean...

Would have been nice to get an explanation tho... :-/
0
 
LVL 14

Author Comment

by:Farzad Akbarnejad
ID: 9673121
Hi,
In the

// Section 1
          System.out.println("In Section 1");
          for (int i = 0; i <= 3; i++) {
               for (int j = 0; j<=2; j++) {
                    try {
                         System.out.println(intArray[i][j]);
                    } catch (Exception e) {
                         System.out.println("N/A");
                    }
               }
          }

I want to exit outer loop whenever rich Exception.
Also in the

          try {
               for (int i = 0; i <= 3; i++)
                    for (int j = 0; j<=2; j++)
                         System.out.println(intArray[i][j]);
          } catch (Exception e) {
               System.out.println("N/A");
          }

I want to contine next value of i, j whenever rich Exception.

-FA
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9673235
The first gets out completely at i == 2, the second passes control back to the outer loop at j == 1:


  static void breakOuter() {
    System.out.println("In Section 1");
    OUTER:
    for (int i = 0; i <= 3; i++) {
         for (int j = 0; j<=2; j++) {
              try {
                   System.out.println(i + "," + j);
                   if (i == 2) throw new RuntimeException("breaking");

              } catch (Throwable e) {
                   break OUTER;
              }
         }
    }
  }


  static void breakInner() {
    System.out.println("In Section 1");
    for (int i = 0; i <= 3; i++) {
         INNER:
         for (int j = 0; j<=2; j++) {
              try {
                   System.out.println(i + "," + j);
                   if (j == 1) throw new RuntimeException("breaking");

              } catch (Throwable e) {
                   break INNER;
              }
         }
    }
  }
0
 
LVL 14

Author Comment

by:Farzad Akbarnejad
ID: 9673345
Hello,
I don't know what elements of array are N/A. Some random holes are in array list. I want to find those holes. (I print N/A for those holes) Using (i == 1) or (j ==2) means that you know place of holes!!

Thanks
-FA
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9674285
Not sure what you mean by that...
0
 
LVL 14

Author Comment

by:Farzad Akbarnejad
ID: 9675845
Hi,

you use

if (j == 1) throw new RuntimeException("breaking");

in your code. Why (j == 1)? Because you noticed:

          intArray[0] = new int[1];
          intArray[1] = new int[3];
          intArray[2] = new int[2];
          intArray[3] = new int[3];

But the above array is only a test bench. So I want to use
'ArrayIndexOutOfBoundsException' to find where we don't allocate memory for array.

-FA

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9677887
>>Why (j == 1)?

Just as an example. ArrayIndexOutOfBoundsException will be fine to check the bounds - just change it.

0
 
LVL 14

Author Comment

by:Farzad Akbarnejad
ID: 9683346
Hello,
I modified my code by help of your code. Now it has four sections:

1. "Section 1 - CEHJ"
2. "Section 1 - FarzadA"
3. "Section 2 - FarzadA"
4. "Section 2 - CEHJ"

"Section 1 - CEHJ" is as same as "Section 1 - FarzadA" it is ok and thanks.

"Section 2 - CEHJ" is as same as "Section 2 - FarzadA" but
"Section 2 - CEHJ" doesn't continue to end of code and breaks the Application.

This is the main problem that caused to I ask this question.
Would you please help me?


// CODE CODE CODE CODE CODE CODE CODE CODE CODE

public class TestClass {
      public static void main(String args[]) {
            int[][] intArray = new int[4][];
            intArray[0] = new int[1];
            intArray[1] = new int[3];
            intArray[2] = new int[2];
            intArray[3] = new int[3];

            intArray[0][0] = 12;
            intArray[1][2] = 16;
            intArray[2][1] = 17;
            intArray[3][2] = 15;

            // Section 1 - CEHJ
            System.out.println("In Section 1 - CEHJ");
            for (int i = 0; i <= 3; i++) {
                  INNER:
                  for (int j = 0; j<=2; j++) {
                        try {
                              System.out.println("intArray["+i+"]["+j+"]="+intArray[i][j]);
                        } catch (ArrayIndexOutOfBoundsException e) {
                              System.out.println("intArray["+i+"]["+j+"]=N/A");
                        } catch (Throwable e) {
                              break INNER;
                        }
                  }
            }

            // Section 1 - FarzadA
            System.out.println("In Section 1 - FarzadA");
            for (int i = 0; i <= 3; i++) {
                  for (int j = 0; j<=2; j++) {
                        try {
                              System.out.println("intArray["+i+"]["+j+"]="+intArray[i][j]);
                        } catch (Exception e) {
                              System.out.println("intArray["+i+"]["+j+"]=N/A");
                        }
                  }
            }

            // Section 2 - FarzadA
            System.out.println("In Section 1 - FarzadA");
            try {
               for (int i = 0; i <= 3; i++)
                        for (int j = 0; j<=2; j++)
                               System.out.println(intArray[i][j]);
            } catch (Exception e) {
               System.out.println("N/A");
            }

            // Section 2 - CEHJ
            System.out.println("In Section 1 - CEHJ");
            OUTER:
            for (int i = 0; i <= 3; i++) {
                  for (int j = 0; j<=2; j++) {
                        try {
                              System.out.println("intArray["+i+"]["+j+"]="+intArray[i][j]);
                        } catch (ArrayIndexOutOfBoundsException e) {
                              System.out.println("intArray["+i+"]["+j+"]=N/A");
                              throw new RuntimeException("breaking");
                        } catch (Throwable e) {
                              break OUTER;
                        }
                  }
            }

            System.out.println("End of Test.");
      }
}

// CODE ENDED CODE ENDED CODE ENDED CODE ENDED

Thanks
-FA
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9686454
That's because you don't have an outer exception block. The exception thrown here:

>>
                         throw new RuntimeException("breaking");
                    } catch (Throwable e) {
                         break OUTER;
                    }
>>

will NOT be caught in the catch(Throwable... statement as that is not a block. The exception is therefore uncaught, interrupting the program flow. In order for it to continue, you'd have to have:

          System.out.println("In Section 1 - CEHJ");
          OUTER:
          for (int i = 0; i <= 3; i++) {
               for (int j = 0; j<=2; j++) {
                    try {
                        try {
                             System.out.println("intArray["+i+"]["+j+"]="+intArray[i][j]);
                        } catch (ArrayIndexOutOfBoundsException e) {
                             System.out.println("intArray["+i+"]["+j+"]=N/A");
                             throw new RuntimeException("Index out of bounds - going into outer exception block...");
                        }
                    }
                    catch (Throwable e) {
                           System.err.println("Throwable caught here. Message was: '" + e.getMessage() + "'");
                           break OUTER;
                    }
               }
          }


Or, more simply:

          System.out.println("In Section 1 - CEHJ");
          OUTER:
          for (int i = 0; i <= 3; i++) {
               for (int j = 0; j<=2; j++) {
                    try {
                         System.out.println("intArray["+i+"]["+j+"]="+intArray[i][j]);
                    } catch (ArrayIndexOutOfBoundsException e) {
                         System.out.println("intArray["+i+"]["+j+"]=N/A");
                         break OUTER;
                    }
               }
          }


0
 
LVL 14

Author Comment

by:Farzad Akbarnejad
ID: 9692207
Thanks CEHJ for your help. I learned more about Exceptions.

-FA
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9693453
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question