Solved

Exit loop placed in Try-Catch using Break and Continue

Posted on 2003-10-30
15
604 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 85
HSSFWorkbook cannot be resolved error 10 69
Spring Framework HTTPSession management 1 35
more than one jdk and one jre 1 41
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

777 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