Exception Handling Problem Again!

Soldato
Joined
27 Aug 2004
Posts
17,111
Location
Geordieland
Hi guys, Ive got it working properly now bar one thing. Its works fine, but on the first catch I want it to just say 'Exception Caught' but its saying Excpetion Caught and then giving me the exception error anyway.

Code:
public class ExceptionHomework
{
    Scanner scan ;
    int data;
    int slot;
    int[] value;

	boolean done = false;

    public ExceptionHomework()
    {
        scan = new Scanner( System.in );
        data=0;
        slot=0 ;
        value = new int[10];
    }

    public void Data()
    {
    	do
    	{
    		try
    		{
      			System.out.print("Enter the data: ");
      			data = scan.nextInt();
      		}
      		
      		catch (InputMismatchException inm)
			{
		 		System.out.println("Exception error "+inm.toString()+ " caught\n");
			}
			
			try
			{
			    System.out.print("Enter the array index: ");
      			slot = scan.nextInt();
      			value[slot] = data;		
			}

			catch(ArrayIndexOutOfBoundsException oob)
			{
				System.out.println("Exception "+oob.toString()+ " caught\n");
			}
			
		}
		while(!done);
	}
}

Code:
public class ExceptionHomeworkDriver
{
	public static void main ( String [] args)
	{
	     ExceptionHomework exHmwk = new  ExceptionHomework();
	     exHmwk.Data();
	     System.out.println("Good Bye");
	}
}

Can anyone give me a hint as to why its catching the error and then letting it go through the catch.

Cheers for any help.
 
Hi,

I think the problem might be that once the first exception has been caught the code will then fall down through into the next try anyway. As the first assignment of data wasn't successful this will fail too. Unless your catch throws an exception itself, the code below it will be reached.

You could structure your code into one try, catching both exceptions at the end of the try:

Code:
try {
    // Enter the data here.
    // Enter the slot here.
} catch (InputMismatchException ex) {
    ....
} catch (ArrayIndexOutOfBoundsException ex) {
 ....
}

That way any exception will cause the rest of the code not to be executed.

You might also want to consider getting your Data method to throw an exception so the calling code knows something went wrong.

Hope that helps,
Jim
 
Worth noting that both of the exception types you are catching could be prevalidated rather than relying on exception handling. Try catch blocks are actually somewhat resource heavy from a compilation point of view so it's better to reserve there use for errors that can't be anticipated or when dealing with resources ie files where you can never guarantee fully a non error state ie you can test a file exists but it may be deleted before your next line of code when you try to access it :)
 
pinkaardvark said:
Worth noting that both of the exception types you are catching could be prevalidated rather than relying on exception handling. Try catch blocks are actually somewhat resource heavy from a compilation point of view so it's better to reserve there use for errors that can't be anticipated or when dealing with resources ie files where you can never guarantee fully a non error state ie you can test a file exists but it may be deleted before your next line of code when you try to access it :)

But you'd still need the try/catch blocks as the functions themselves specify that those exceptions may be thrown. Even if you have sufficient validation that you know they wont be thrown in practice,the function signatures say that they *might* be thrown, so you need to catch them, or specify the exceptions in the calling functions signature.
 
you're dumping the entire exception.. have a look at the API for exception objects, there will be a name() method or similar so you don't have to dump the trace.
 
Back
Top Bottom