class BadFoodException extends Exception{
//Do something
}
class Reader {
private String food1;
private String food2;
public static void main(String args[])
{
Reader m = new Reader(args);
for (int i=0; i<args.length; i++){
try
{
m.checkfood(args[i]);
}
catch(BadFoodException e){System.out.println(args[i]+ " caught");}
}
}
private Reader(String [] args){
food1=args[0];
food2=args[1];
}
void checkfood(String food) throws BadFoodException
{ if( food == "banana")
throw new BadFoodException();
System.out.println(food + " passed through.");
}
}
I am passing two foods- banana and mango through command line. The checkfood method should throw an exception when the food is banana.. and the mango should pass through. However both are passing through instead? why?