Click here to watch in Youtube :
https://www.youtube.com/watch?v=vuM2MFXvnPo&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial : Java Exception handling (throw keyword) |
public class ThrowDemo { public static void main(String[] args) { ThrowDemo throwDemo = new ThrowDemo(); throwDemo.validateAge(15); System.out.println("Validated.."); } public void validateAge(int age) { if (age < 25) { throw new ArithmeticException("Not eligible to apply for Govt Jobs"); } else { System.out.println("Eligible to apply for Govt Jobs"); } } }
Exception in thread "main" java.lang.ArithmeticException: Not eligible to apply for Govt Jobs at ThrowDemo.validateAge(ThrowDemo.java:15) at ThrowDemo.main(ThrowDemo.java:7)
public class ThrowHandlingDemo { public static void main(String[] args) { ThrowHandlingDemo throwHandlingDemo = new ThrowHandlingDemo(); try { throwHandlingDemo.validateAge(15); } catch(ArithmeticException arithmeticException) { System.out.println(arithmeticException.getMessage()); } System.out.println("Validated.."); } public void validateAge(int age) { if (age < 25) { throw new ArithmeticException("Not eligible to apply for Govt Jobs"); } else { System.out.println("Eligible to apply for Govt Jobs."); } } }
Not eligible to apply for Govt Jobs Validated..
https://sites.google.com/site/javaee4321/java/ExceptionDemo_throw_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_throw_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/969ffd88eb34c4fd42d966118b27490392b78865/BasicJava/ExceptionDemo_throw_App/?at=master
See also:
No comments:
Post a Comment