Thursday 23 June 2016

Java Tutorial : Java Exception handling (throw keyword)


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) 
ThrowDemo.java
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");
        }
    }
}
Output
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)
ThrowHandlingDemo.java
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.");
        }
    }

}
Output
Not eligible to apply for Govt Jobs
Validated..
Click the below link to download the code:
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:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment