public class TryCatchTest {

	public static void MyNumber(String x) throws TryCatchException1{
	
	try{
		
			String y = x.substring(0,200);
			
		}catch(TryCatchException1 e){
		
			System.out.println("BBBBB");
			System.out.println(e.getMessage());
		//	e.printStackTrace();
		}catch(java.lang.StringIndexOutOfBoundsException e){
			System.out.println("AAAAAAA");
			e.printStackTrace();
			e.getMessage();
		}

	}
	
}

class TryCatchException1 extends java.lang.StringIndexOutOfBoundsException{

	TryCatchException1(){
		
		super();
	}

	TryCatchException1(String msg){
		super(msg);
	}
	

}

why is my exception not caught on the first block of type TryCatchException1 ??? can't figure it out help..help.. my driver class just calls
TryCatchTest.MyNumber("myString");

thanks.

Because the x.substring(0,200) throws a StringIndexOutOfBoundsException not a TryCatchException1.

If things worked the way you thought then when people do this:

try {

} catch (NumberFormatException e) {

} catch (SQLException e) {

} catch (Exception e) {

}

Then the other exceptions will never be caught and everything would go to the last Exception. It is true that you extend the StringIndexOutOfBoundsException, but that method doesn't throw the TryCatchException1.
Look at this:

public void methTryCatch() throws  TryCatchException1 {
   throw new TryCatchException1("TryCatchException1");
}

public void methOutOfBounds() throws StringIndexOutOfBoundsException {
   throw new StringIndexOutOfBoundsException("StringIndexOutOfBoundsException");
}
main() {
  try {
       methTryCatch();
  } catch (TryCatchException1 e) {
      System.out.println("methTryCatch: "+e.getMessage());
  } catch (StringIndexOutOfBoundsException e) {
      System.out.println("methTryCatch: "+e.getMessage());
  }

try {
       methOutOfBounds();
  } catch (TryCatchException1 e) {
      System.out.println("methOutOfBounds: "+e.getMessage());
  } catch (StringIndexOutOfBoundsException e) {
      System.out.println("methOutOfBounds: "+e.getMessage());
  }

try {
       methTryCatch();
  } catch (StringIndexOutOfBoundsException e) {
      System.out.println("methTryCatch: "+e.getMessage());
  }
}

Also since your method throws a TryCatchException1 you can do this:

public static void MyNumber(String x) throws TryCatchException1{
	try{
		
			String y = x.substring(0,200);
			
		} catch(java.lang.StringIndexOutOfBoundsException e){
                        throw new TryCatchException1(e.getMessage());
		}

	}

OR

public static void MyNumber(String x) throws TryCatchException1{
			String y = x.substring(0,200);
	}

Try catching the above like this and see what happens:

try{
		
			MyNumber("myStirng");
			
		}catch(TryCatchException1 e){
			System.out.println("BBBBB: "+e.getMessage());
		}catch(java.lang.StringIndexOutOfBoundsException e){
			System.out.println("AAAAAAA: "+e.getMessage());
		}

Edited 6 Years Ago by javaAddict: n/a

thanks javaddict ::):) I appreciate your example...very clear and also very helpful..

What i don't get is.. running the first of the last two pieces of codes,

public static void MyNumber(String x) throws TryCatchException1{	
		String y = x.substring(0,200);	}

I get the output ..

AAAAAA : String index out of range: 200

of type StringIndexOutOfBoundsException. why is that happening? why not of type TryCatchException1...it is the type thrown so i'm wondering why :):) :) ..thanks for the help :)

Edited 6 Years Ago by zyaday: n/a

http://java.sun.com/docs/books/tutorial/essential/exceptions/throwing.html

Look at the pop() method example they have there. Since the substring() method throws a StringIndexOutOfBoundsException, which you did not catch, it was passed down to wherever you called MyNumber(). Additionally you would need to say throw new TryCatchException1() to actually throw an Exception. The throws clause in your method header only specifies that the caller should be aware of the possibility that an Exception will be thrown, and it tells the caller what type of Exception may be thrown. By not catching the index out of bounds exception and letting the Exception propagate back to wherever you called MyNumber from, you're pretty much lying to the caller of your method.


Now consider my example. (Note: I would not normally hide the result of parsing the int, however, this is to demonstrate that the method is consistent with its method header, and also to demonstrate one scenario in which you might want to declare your method header as throws some exception.

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Test
{
	static int[] array = new int[15];
	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		String index = input.next();
		try{
			someMethod(index);
			System.out.println("Index was in range!");
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("Index was out of range :(");
		}
	}
	
	public static void someMethod(String index) throws ArrayIndexOutOfBoundsException{
			int i = 0;
			try{
				i = Integer.parseInt("Not an integer");
			} catch(Exception e){}
			
			if (i > array.length-1 || i < 0) throw new ArrayIndexOutOfBoundsException(index);
			
	}

	
}

Edited 6 Years Ago by BestJewSinceJC: n/a

thanks javaddict ::):) I appreciate your example...very clear and also very helpful..

What i don't get is.. running the first of the last two pieces of codes,

public static void MyNumber(String x) throws TryCatchException1{	
		String y = x.substring(0,200);	}

I get the output ..

AAAAAA : String index out of range: 200

of type StringIndexOutOfBoundsException. why is that happening? why not of type TryCatchException1...it is the type thrown so i'm wondering why :):) :) ..thanks for the help :)

Because in the last example even though you declare the method to throw a TryCatchException1, the x.substring(0,200) threw a StringIndexOutOfBoundsException . So that was caught.

so when we put our method header as throws exception, has no consequence in terms of results at runtime ... but its just to pre-inform the calling method to get prepared for that kind of exception..hmm..i think i got it now!

thanks guru javaddict....

The throws clause in your method header only specifies that the caller should be aware of the possibility that an Exception will be thrown, and it tells the caller what type of Exception may be thrown.

so when we put our method header as throws exception, has no consequence in terms of results at runtime ... but its just to pre-inform the calling method to get prepared for that kind of exception..hmm..i think i got it now!

thanks guru javaddict....

...

This article has been dead for over six months. Start a new discussion instead.