public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setAge(int age) throws MyException {
if (age <= 0) {
throw new MyException("Age must be greater than 0.");
}
this.age += age;
}
public int getAge() {
return age;
}
}
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("John", 30);
try {
person.setAge(-10);
} catch (MyException e) {
System.out.println(e.getMessage());
}
System.out.println(person.getAge());
}
}