For instance,
public class Adder
{
public static void main(String[] args)
{
double result;
try
{
result = add(args);
System.out.println(args[0]+"+"+args[1]+": "+result);
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Too few operands.");
}
}
public static double add(String[] terms)
throws ArrayIndexOutOfBoundsException
{
double left, result, right;
String operator;
left = 0.0;
right = 0.0;
try
{
left = Double.parseDouble(terms[0]);
}
catch (NumberFormatException nfe)
{
left = 0.0;
}
try
{
right = Double.parseDouble(terms[1]);
}
catch (NumberFormatException nfe)
{
right = 0.0;
}
result = right + left;
return result;
}
}
Why isn't just having a try catch block good enough?
public class Adder
{
public static void main(String[] args)
{
double result;
try
{
result = add(args);
System.out.println(args[0]+"+"+args[1]+": "+result);
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Too few operands.");
}
}
public static double add(String[] terms)
throws ArrayIndexOutOfBoundsException
{
double left, result, right;
String operator;
left = 0.0;
right = 0.0;
try
{
left = Double.parseDouble(terms[0]);
}
catch (NumberFormatException nfe)
{
left = 0.0;
}
try
{
right = Double.parseDouble(terms[1]);
}
catch (NumberFormatException nfe)
{
right = 0.0;
}
result = right + left;
return result;
}
}
Why isn't just having a try catch block good enough?