Automation Using Selenium Webdriver

Thursday, 24 November 2016

throw keyword in java with example


Throw keyword in java
  • Throw keyword is used to throw exception manually.
  • Whenever it is required to suspend the execution of the functionality based on the user defined logical error condition we will use this throw keyword to throw exception.
  • So we need to handle these exceptions also using try catch blocks.

 Java simple example Program to explain use of throw keyword in java


  1. package exceptions;
  2.  
  3. public class ThrowKeyword {
  4.  
  5.     
  6. public static void main(String[] args) {
  7.  
  8. try {
  9.             
  10.    throw new ArithmeticException();
  11.             
  12.             
  13. } catch (Exception e) {
  14.  
  15.    System.out.println(e);
  16.    e.printStackTrace();
  17. }
  18.  
  19. }
  20.  
  21. }

Output:



  1. java.lang.ArithmeticException
  2. java.lang.ArithmeticException
  3.     at exceptions.ThrowKeyword.main(ThrowKeyword.java:11)



Rules to use "throw" keyword in java 
  • throw keyword must follow Throwable type of object.
  • It must be used only in method logic.
  • Since it is a transfer statement , we can not place statements after throw statement. It leads to compile time error Unreachable code




  •  We can throw user defined exception using throw keyword.

  1. //Custom exception
  2. package com.instanceofjavaforus;
  3. public class InvalidAgeException extends Exception {
  4.  InvaidAgeException(String msg){
  5.  super(msg);
  6.  }
  7. }
     

  1. package com.exceptions;
  2. public class ThrowDemo {

  3. public boolean isValidForVote(int age){
  4.  
  5.  try{
  6.    if(age<18){
  7.   throw new InvalidAgeException ("Invalid age for voting");
  8. }
  9.  }catch(Exception e){
  10.  System.out.println(e);
  11.  }
  12.   return false;
  13.  }
  14.  
  15. public static void main(String agrs[]){
  16.  
  17.  ThrowDemo obj= new ThrowDemo();
  18.    obj.isValidForVote(17);
  19.  
  20.   }
  21. }


 Output:


  1.  exceptions.InvalidAgeException: Invalid age for voting
  • We can throw predefined exceptions using throw keyword


  1. package com.instanceofjava;
  2.  
  3. public class ThrowKeyword{
  4. public void method(){
  5.  
  6.  try{
  7.   
  8.   throw new NullPointerException("Invalid age for voting");
  9. }
  10.  }catch(Exception e){
  11.  System.out.println(e);
  12.  }
  13.  }
  14.  
  15.   public static void main(String agrs[]){
  16.  
  17.  ThrowKeyword obj= new ThrowKeyword();
  18.  
  19.    obj.method();
  20.  
  21.   }
  22. }

 Output:


  1.  java.lang.NullPointerException: Invalid age for voting

Top 15 Java abstract class and abstract method interview questions and programs


1.What is abstract class in java?
  • Hiding the implementation  and showing the function definition to the user.
  • Abstract class contains abstract methods and concrete methods(normal methods)
2.How can we define an abstract class? 
  • Using abstract keyword we can define abstract class.
  • Check below code for abstract class example program. 

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   //
  5. }


3. How to declare an abstract method?

  • By Using abstract keyword in the method signature we can declare abstract method.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   abstract void show(); 

  5. }

 4. Can we define abstract class without abstract method?

  •  Yes we can define abstract class without abstract methods.
  • It is not mandatory to create at-least one abstract method in abstract class.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.  

  5. }

 Read more at Can you define an abstract class without any abstract methods? if yes what is the use of it?


 5.Can we create object object for abstract class?
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly.
  • Through sub class abstract class members will get memory. Whenever we create sub class object of abstract class  abstract class object will be created. i.e abstract class members will get memory at that time.


  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractClass obj= new AbstractClass (); 
  13. // ERROR: Cannot instantiate the type AbstractClass
  14.  
  15. }
  16. }

 6. Is is possible to declare abstract method as static?
  • No. its not possible to declare abstract method with static keyword.
  • If we declare abstract method with static compiler will throw an error.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }




7.Can we declare abstract method as final?
  • No. its not possible to declare abstract method with final keyword.
  • If we declare abstract method with final compiler will throw an error.
  • Because abstract methods should be override by its sub classes.


  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }



8. Is it possible to declare abstract method as private?
  • No. its not possible to declare abstract method with private .
  • If we declare abstract method with private compiler will throw an error.
  • Because abstract methods should be override by its sub classes.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     private abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
9. Is it possible to declare abstract method as public ?
  • Yes we can declare abstract methods as public.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     public abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
10. Is it possible to declare abstract method with default?
  • Yes we can declare abstract methods with default.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.    abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

11. Is it possible to declare abstract method with protected modifier?
  • Yes we can declare abstract methods as protected.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     protected abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 

12.What are the valid and invalid keywords or modifier with abstract class?
  • public,  protected and default are valid.
  • static,  final and private are invalid.

13.Can abstract method declaration include throws clause?
  • Yes. We can define an abstract class with throws clause.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     abstract void add() throws Exception; 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
 14.What happens if sub class not overriding abstract methods?
  • If sub class which is extending abstract class not overriding abstract method compiler will throw an error.
  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public class Sample extends AbstractClass {
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }
15. Can we escape of overriding abstract class in sub class which is extending abstract class?
  • Yes we can escape from overriding abstract method from super abstract class by making our class again as abstract.
  • The class which is extending our class will get responsibility of overriding all abstract methods in our class and in super class.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public abstract class Sample extends AbstractClass {
  3.  
  4.    
  5. }

  1. package Abstraction;
  2. public class Example extends Sample{
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }

You Might Like: