• Post Reply Bookmark Topic Watch Topic
  • New Topic

throw clause in exceptions  RSS feed

 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to test a condition where it throws a particular exception. Since I am not able to re-create in real-time(I get the exception when say a server is down), I am throwing the exception. IS this the way to do it ? I must be doing something wrong because it DOES NOT come into the catch block and executing the else part. Here is the sample Code. what am I doing wrong ?

try
{
//This is the original method that throws the exception when system is down
//Query.methodA
throw new SubValidateException();

} catch (SubValidateException ve)
{


String[] errCodes[] = null;
errCodes[0] = null;


if (errCodes[0] != null && errCodes[0].equals("SearchSomething"))
{
System.out.println("Inside if- catch loop");
}
else {
System.out.println("Inside else- catch loop");
}

}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're throwing it correctly (assuming SubValidateException extends Exception, or at least Throwable).

As the first statement in your catch block, add a line...

System.out.println("SubValidateException caught!");

I think this will show you that it's being caught too.

However, you're going to get a NullPointerException at runtime by setting your 2-dimensional errCodes Array to null and then trying to assign a value to a member before the Array is actually created. I think this is where the real issue is.

Do you really want a 2-dimensional Array? Or do you actually want...

[ October 01, 2004: Message edited by: marc weber ]
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely right! It does come inside the catch block. That was stupid of me to initialise the string array that way! Thanks a bunch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!