Skip to content

Commit

Permalink
java-practical-04-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajjitlai committed Oct 14, 2024
1 parent 2560937 commit 5b65efb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Binary file added Java/Practical/practical-04/UnderAge.class
Binary file not shown.
Binary file added Java/Practical/practical-04/exceptionDemo.class
Binary file not shown.
37 changes: 37 additions & 0 deletions Java/Practical/practical-04/exceptionDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class UnderAge extends Exception {
private int age;

public UnderAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Under Age: " + age;
}
}

public class exceptionDemo {
public void test(int age) throws UnderAge {
if (age < 18) {
throw new UnderAge(age);
} else {
System.out.println("Age is valid: " + age);
}
}

public static void main(String[] args) {
exceptionDemo demo = new exceptionDemo();
try {
demo.test(16);
} catch (UnderAge e) {
System.out.println(e);
}

try {
demo.test(20);
} catch (UnderAge e) {
System.out.println(e);
}
}
}

0 comments on commit 5b65efb

Please sign in to comment.