Solved

Exit loop placed in Try-Catch using Break and Continue

Posted on 2003-10-30
15
602 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 51
strDist challenge 35 84
topping2 challenge 13 59
Java Jpanels and Jframe 8 20
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now