What is the result?

Given:

class InvalidAgeException extends IllegalArgumentException { }

public class Tracker {

void verify (int age) throws IllegalArgumentException {

if (age < 12)

throw new InvalidAgeException ();

if (age >= 12 && age <= 60)

System.out.print(“General category”);

else

System.out.print(“Senior citizen category”);

}

public static void main(String[] args) {

int age = Integer.parseInt(args[1]);

try {

new Tracker().verify(age);

}

catch (Exception e) {

System.out.print(e.getClass());

}

}

}

And the command-line invocation:

Java Tracker 12 11

What is the result?

Given:

class InvalidAgeException extends IllegalArgumentException { }

public class Tracker {

void verify (int age) throws IllegalArgumentException {

if (age < 12)

throw new InvalidAgeException ();

if (age >= 12 && age <= 60)

System.out.print(“General category”);

else

System.out.print(“Senior citizen category”);

}

public static void main(String[] args) {

int age = Integer.parseInt(args[1]);

try {

new Tracker().verify(age);

}

catch (Exception e) {

System.out.print(e.getClass());

}

}

}

And the command-line invocation:

Java Tracker 12 11

What is the result?

A.
General category

B.
class invalidAgeException

C.
class java.lang.IllegalArgumentntException

D.
class java.lang.RuntimeException

Explanation:
The second argument 11 makes the program to throw an InvalidAgeException due to the line:

if (age < 12)
throw new InvalidAgeException ();



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Charlie

Charlie

getClass() returns the runtime class of an object.

Jav

Jav

B class invalidAgeException