2 catch statements for an IllegalArgumentException. - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# 2 catch statements for an IllegalArgumentException.

Posts: 13Member
How do I incorporate 2 catch statements for a IllegalArgumentExceptions in a factorials program?

Can you embed a if else statement in a catch block?

[code]

import java.util.Scanner;

public class Factorials
{
public static void main(String[] args)
{ try
{
String keepGoing = "y";
Scanner scan = new Scanner(System.in);

while (keepGoing.equals("y") || keepGoing.equals("Y"))
{
System.out.print("Enter an integer: ");
int val = scan.nextInt();
System.out.println("Factorial(" + val + ") = "
+ MathUtils.factorial(val));

System.out.print("Another factorial? (y/n) ");
keepGoing = scan.next();
}
}
catch (IllegalArgumentException ex)
{
System.out.println("Integer must be greater than 0");
}
catch (IllegalArgumentException ex)
{
System.out.println ("Integer must be less than 17");
}
}
}[/code]

[code]public class MathUtils
{
//-------------------------------------------------------------
// Returns the factorial of the argument given
//-------------------------------------------------------------
public static int factorial(int n) {
if (n < 0)
throw new IllegalArgumentException("Integer must be greater than 0");
else if (n > 16)
throw new IllegalArgumentException("Integer must less than 17");

{
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}
}[/code]