-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |