Throwing exception makes window freeze

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idlackage
    New Member
    • Jun 2010
    • 1

    Throwing exception makes window freeze

    I have some code to draw shapes, and I want to check that the number of shapes drawn are not over the amount of room in the array.

    Code:
    // In the constructor:
    // shapeObjects = new Shape[ 10 ]; // Example length of array contains ten shapes
    // numOfShapes = 0;
    
    public void paintComponent( Graphics g ) {
        
        // Inherit the paintComponent method from the superclass
        super.paintComponent( g );
        
        try {
          
          // Check whether or not the shape array is already full
          if( numOfShapes == shapeObjects.length ) {
            throw new ArrayIndexOutOfBoundsException();
          } else {
            // If shape is null, do nothing
            if ( shape == null ) {
              return;
            } else {
              // Otherwise, draw the shapes
              for( int i = 0; i < numOfShapes; i++ ) {
                shapeObjects[i].draw( g );
              }
              shape.draw( g );
            }
          }
          
        } catch( ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException ) {
          JOptionPane.showMessageDialog( null, "Number of shapes drawn cannot exceed 100.", "Error", JOptionPane.ERROR_MESSAGE );
        }
        
      }
    Screenshot of the freezing if necessary: image
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Are you sure it doesn't just clear the component? If you throw the exception, none of them redraw.

    Are you getting the error window?

    Comment

    Working...