1.
Define Exceptions VowelException, BlankException, ExitException to
restrict the input of vowel, space and ‘X’. Write another class
TestException which reads a character from command line. If it is a vowel,
throw VowelException, if it is a blank space throw BlankException and for
a character ‘X’ throw an ExitException and terminate the program. For any
other character, display “Valid character”.
// [Link]
class VowelException extends Exception {
public VowelException(String message) {
super(message);
}
}
// [Link]
class BlankException extends Exception {
public BlankException(String message) {
super(message);
}
}
// [Link]
class ExitException extends Exception {
public ExitException(String message) {
super(message);
}
}
// [Link]
public class TestException {
public static void main(String[] args) {
if ([Link] == 0) {
[Link]("Please provide a character as a command-line argument.");
return;
}
char ch = args[0].charAt(0);
try {
if ([Link](ch) == 'a' || [Link](ch) == 'e' ||
[Link](ch) == 'i' || [Link](ch) == 'o' ||
[Link](ch) == 'u') {
throw new VowelException("Vowel character is not allowed.");
} else if (ch == ' ') {
throw new BlankException("Blank space is not allowed.");
} else if (ch == 'X' || ch == 'x') {
throw new ExitException("Exit character 'X' entered. Program will terminate.");
} else {
[Link]("Valid character.");
}
} catch (VowelException | BlankException | ExitException e) {
[Link]("Exception: " + [Link]());
// Explicitly exit on ExitException
if (e instanceof ExitException) {
[Link](0);
}
}
}
}
Output:-
2. Write a program which accepts two integers and an arithmetic operator
from the command line and performs the operation. Check the following
user defined exceptions:
i. If the number of arguments are less than 3 then throw
“FewArgumentsException”.
ii. If the operator is not an Arithmetic operator, throw
“InvalidOperatorException”.
iii. If result is –ve, then throw “NegativeResult” exception.
// [Link]
class FewArgumentsException extends Exception {
public FewArgumentsException(String message) {
super(message);
}
}
// [Link]
class InvalidOperatorException extends Exception {
public InvalidOperatorException(String message) {
super(message);
}
}
// [Link]
class NegativeResultException extends Exception {
public NegativeResultException(String message) {
super(message);
}
}
// [Link]
public class ArithmeticOperation {
public static void main(String[] args) {
try {
// Check if there are exactly 3 arguments
if ([Link] < 3) {
throw new FewArgumentsException("Error: Fewer than 3 arguments provided.");
}
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
String operator = args[2];
int result;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
result = num1 / num2;
break;
default:
throw new InvalidOperatorException("Invalid operator. Use +, -, *, or /.");
}
if (result < 0) {
throw new NegativeResultException("Result is negative: " + result);
}
[Link]("Result: " + result);
} catch (FewArgumentsException | InvalidOperatorException | NegativeResultException e) {
[Link]("Exception: " + [Link]());
} catch (NumberFormatException e) {
[Link]("Error: Please enter valid integers.");
} catch (ArithmeticException e) {
[Link]("Error: " + [Link]());
}
}
}
4. Define class MyDate with members day, month and year. Define default
and parameterized constructors. Accept values from the command line and
create a date object. Throw user defined exceptions –
“InvalidDayException” or “InvalidMonthException” if the day or month
are invalid. If the date is valid, display message “Valid Date”.
// [Link]
class InvalidDayException extends Exception {
public InvalidDayException(String message) {
super(message);
}
}
// [Link]
class InvalidMonthException extends Exception {
public InvalidMonthException(String message) {
super(message);
}
}
// [Link]
public class MyDate {
int day;
int month;
int year;
// Default constructor
public MyDate() {
[Link] = 1;
[Link] = 1;
[Link] = 2000;
}
// Parameterized constructor with exception handling
public MyDate(int day, int month, int year)
throws InvalidDayException, InvalidMonthException {
if (month < 1 || month > 12) {
throw new InvalidMonthException("Month must be between 1 and 12.");
}
if (day < 1 || day > 31) {
throw new InvalidDayException("Day must be between 1 and 31.");
}
// Optional: Handle months with less than 31 days
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
throw new InvalidDayException("This month has only 30 days.");
}
if (month == 2) {
boolean leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > (leap ? 29 : 28)) {
throw new InvalidDayException("Invalid day for February.");
}
}
[Link] = day;
[Link] = month;
[Link] = year;
}
public void displayDate() {
[Link]("Valid Date: " + day + "/" + month + "/" + year);
}
}
// [Link]
public class TestDate {
public static void main(String[] args) {
try {
if ([Link] < 3) {
[Link]("Please enter day, month, and year as command-line arguments.");
return;
}
int day = [Link](args[0]);
int month = [Link](args[1]);
int year = [Link](args[2]);
MyDate date = new MyDate(day, month, year);
[Link]();
} catch (InvalidDayException | InvalidMonthException e) {
[Link]("Exception: " + [Link]());
} catch (NumberFormatException e) {
[Link]("Please enter valid integers for day, month, and year.");
}
}
}
Output:-