Answer:
Check the explanation
Explanation:
public class Main {
public static void main(String[] args) {
//execution starts from here so initializing the try block for errors
try{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a file name: ");
String name = keyboard.next();
//the line below throws file not found exception if the given file name is not found or unable to open
Scanner file = new Scanner(new File(name));
String line = file.nextLine();
int n = file.nextInt();
//the below print line function call throws ParameterNotAllowedException if n is zero
printLine(line, n);
}//all Exceptions potentially thrown
catch(InputMismatchException e){
System.out.println("User input type mismatch\n"+e);
}catch(FileNotFoundException e){
System.out.println("Given file name is wrong or does not match");
}catch(ParameterNotAllowedException e){
System.out.println("The n given to printLine must be more than zero");
}catch(IOException ex){
System.err.println("An IOException was caught!");
}
}
//method from above goes here.
public static void printLine(String line, int numberOfTimes) throws Exception {
if (numberOfTimes <=0)
throw new ParameterNotAllowedException("Error. invalid parameter.",numberOFTimes);
for( int i=0; i<numberOfTimes; i++)
System.out.println(line);
}
}
For the user written custom Exception ParameterNotAllowedException. We need to add an exceptional class.
Which is missing from the given info:
class ParameterNotAllowedException extends Exception
{
public ParameterNotAllowedException(String s,int n)
{
// Call constructor of parent Exception
super(s);
}
}