Tuesday, February 11, 2020

Throw and user defined exceptions

"throw ", User-defined Exception Handling

Java provides an excellent Exception Handling mechanism which makes Java highly robust and powerful programming language. Java provides exception handling by try-catch, throws, throw and finally keywords. 

"throw keyword" in Exception Handling

System generated exceptions are thrown automatically and the programmer need not worry about this. However, sometimes you may also yield to throw Exception explicitly. This can be performed by using the “throw” keyword.

Syntax to use "throw":
  throw ThrowableInstance
The ThrowableInstance must be an object of Throwable or its any subclass.

class TDemo{
      public String testThrow(int age) throws Exception
      {
            if(age>=18)
            {
                  return "Valid age";
            }
            else
            {
                  throw new Exception(); //throwing exception
            }                            //at our requirement
      }
}
public class TestThrow {

      public static void main(String[] args) {
            int age=14;    //forcing an exception 
            TDemo td=new TDemo();
            try {//either handle here or use throws
                  System.out.println(td.testThrow(age));
            } catch (Exception e) {
                  System.out.println("Age is not valid");
            }
      }

}
Output:
Age is not valid


User-defined Exceptions

One of the major advantages of the "throw" keyword is to work with user-defined exceptions. Java provides a large and rich set of predefined exceptions. Apart from this rich set of built-in Exceptions, Java provides the ability to the user to define their Own Exception classes. User-defined exceptions provide developers to create their own customized exceptions.

For example, if we want the minimum amount to be filled before starting a bank credit or debit transaction, then we can throw our own user-defined exception if a user has entered an invalid amount. 
user defined exceptions

All user-defined Exceptions must be subclasses of the Exception class or its subclasses. To display meaningful information, the user needs to override the toString() method. 

/***************************************/
public class AgeException extends Exception{//User-defined exception
      @Override
      // customize your exception message
      public String toString() {
            return "Your age is not valid";
      }    
}
/*************class with main method*****/
import java.util.Scanner;

public class UserExceptionDemo {

      public static void main(String[] args) throws AgeException {
            Scanner scan=new Scanner(System.in);
            System.out.println("Enter Age??");
            int age=scan.nextInt();
            if(age>=18)
            {
                  System.out.println("Valid age");
            }
            else
            {
                  throw new AgeException();
            }
      }

}
Output:
Enter Age??
12
Exception in thread "main" Your age is not valid
      at UserExceptionDemo.main(UserExceptionDemo.java:15)

Video Tutorial