Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Exit loop placed in Try-Catch using Break and Continue

Posted on 2003-10-30
15
Medium Priority
?
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

636 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