Catching an exception then printing a msg as a result

Error on my ArrayIndexOutofBoundsException

Page 1 of 1

3 Replies - 1002 Views - Last Post: 25 April 2010 - 05:07 AM Rate Topic: -----

#1 240z240z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 15-March 10

Catching an exception then printing a msg as a result

Posted 25 April 2010 - 04:29 AM

Im receiving an error on my "ArrayIndexOutofBoundsException" line and am humbly asking for help, Ive tried to place brackets in different areas but it doesnt resolve my errors...the following error messages occur:

GoneTooFar.java:13: <identifier> expected
catch(ArrayIndexOutOfBoundsException)
^
GoneTooFar.java:13: '{' expected
catch(ArrayIndexOutOfBoundsException)
^
GoneTooFar.java:16: reached end of file while parsing
}
^
3 errors

VR kent

import javax.swing.*;
public class GoneTooFar
{
   public static void main(String[] args)
  {
   int [] myArray = {10, 20, 30, 40, 50};
	try
	{
	for(int x = 0; x<myArray.length; ++x)
		System.out.println(myArray[x]);
	}
	catch(ArrayIndexOutOfBoundsException)
	System.out.println("Now you've gone too far");
   }
 }



Is This A Good Question/Topic? 0
  • +

Replies To: Catching an exception then printing a msg as a result

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Catching an exception then printing a msg as a result

Posted 25 April 2010 - 04:46 AM

Even if you have one instruction after "catch", you still need brackets ({}):
catch(ArrayIndexOutOfBoundsException)
{ // >> Added }
        System.out.println("Now you've gone too far");
} // End {


And this line is invalid:
catch (ArrayIndexOutOfBoundsException)


because you still need to provide a name for the exception. Try using this:
catch (ArrayIndexOutOfBoundsException e)


For more details, read here.

This post has been edited by sarmanu: 25 April 2010 - 04:46 AM

Was This Post Helpful? 1
  • +
  • -

#3 Krishs  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 105
  • Joined: 06-April 10

Re: Catching an exception then printing a msg as a result

Posted 25 April 2010 - 04:47 AM

following code might help you!!!
 import javax.swing.*;

public class GoneTooFar
{
   public static void main(String[] args)
  {
   int [] myArray = {10, 20, 30, 40, 50};
        try
        {
        for(int x = 0; x<myArray.length; ++x)
                System.out.println(myArray[x]);
        }
        catch(ArrayIndexOutOfBoundsException e) {
        System.out.println("Now you've gone too far");
   }
  }
}


Was This Post Helpful? 0
  • +
  • -

#4 240z240z  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 15-March 10

Re: Catching an exception then printing a msg as a result

Posted 25 April 2010 - 05:07 AM

Thank You Saramanu and Trishs...my "try" was in the wrong place and I was missing a catch name and my brackets were wack yo...this baby works like a charm...again thank you for the assist! Kent
{
   public static void main(String[] args)
  {
   int [] myArray = {10, 20, 30, 40, 50};
	{
	for(int x = 0; x<myArray.length; ++x)
		System.out.println(myArray[x]);
	}
	try
	{
	System.out.println(myArray[6]);
	}
	catch(ArrayIndexOutOfBoundsException e)
	{
	System.out.println("Now you've gone too far");
	}
	}
 }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1