Results 1 to 2 of 2
  1. #1
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default having prob with Exception

    I have following piece of code. I am adding an integer to the 5th index which exceeds the limit of the array. For this, I want to show a user friendly message to the user. But my message "Exception: Array limit exceeded." is not displayed and complier throws exception.

    Java Code:
    	        int array[] = new int[5];
    		
    		for (int i = 0; i < 6; i++) {
    		try{
    			array[i] = i+10;
    		}catch(java.lang.IllegalAccessError ex){
    			System.out.println("Exception: Array limit exceeded.");
    		}
    		}
    - PEACE

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Try this:

    Java Code:
                int array[] = new int[5];
                
            for (int i = 0; i < 6; i++) {
                  try{    
                      array[i] = i+10;
                  }catch(java.lang.ArrayIndexOutOfBoundsException ex){
                    System.out.println("Exception: Array limit exceeded.");
            }
                  }
    EDIT: working and forum browsing don't mix - not 100&#37; if this will fix your problem... there also seems to be extra curly braces, so note - I haven't tried this yet.

    EDIT: Sorry, that'll be the last time I don't pay attention... - it appears you're not catching the correct exception, so catch it. :)
    Last edited by CaptainMorgan; 01-04-2008 at 06:44 PM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  2. Need help on a strange file access prob
    By squad in forum Advanced Java
    Replies: 0
    Last Post: 03-21-2008, 10:55 AM
  3. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  4. Prob with an exercise
    By jhetfield18 in forum New To Java
    Replies: 4
    Last Post: 02-15-2008, 06:11 PM
  5. Threading prob..
    By banie in forum Java Applets
    Replies: 0
    Last Post: 02-05-2008, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •