Solved

Exit loop placed in Try-Catch using Break and Continue

Posted on 2003-10-30
15
608 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 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