Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Multiple catch for try -Exception Already caught Running smoothly..

807601May 7 2008 — edited May 8 2008
take this code for example...

This one runs smoothly..
public class one
{
	public static void main(String args[])
	{
		int i=27;

		try
		{	
			System.out.println(i/0);
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println(e);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}
However if i change the order it would give me a compile time error which in my sun sl-275 book is written it should run fine.

"If the Exception Catch clause is put first, then it would handle all exceptions, and the MyException or MyOtherException catch clauses would never be invoked."

which i think is wrong because the code never gets compiled...
public class one
{
	public static void main(String args[])
	{
		int i=27;

		try
		{	
			System.out.println(i/0);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println(e);
		}
	}
please discuss.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 5 2008
Added on May 7 2008
10 comments
533 views