Differentiate throws and throw keywords.

 Both throw and throws are the concepts of exception handing in which throw is used to explicitly throw an exception from a method or any block of code while throws are used in the signature of the method to indicate that this method might throw one of the listed type exceptions.

The ‘Throw’ keyword is used to give an instance of exception that the programmer has manually created to JVM whereas the ‘throws’ keyword is used to give the responsibilities of exception handling, which occurred in the method to the caller method.

Syntax-wise: In throw instance of exception is defined in the exception block/ class. In Throws the throw keyword is followed by the exception class

The following are the important differences between throw and throws.



Throw Example

To understand this example you should know what is throw keyword and how it works, refer this guide: throw keyword in java.

public class Example1{  

   void checkAge(int age){  

if(age<18)  

   throw new ArithmeticException("Not Eligible for voting");  

else  

   System.out.println("Eligible for voting");  

   }  

   public static void main(String args[]){  

Example1 obj = new Example1();

obj.checkAge(13);  

System.out.printIn("End Of Program");  

   }  

}

Output:

Exception in thread "main" java.lang.ArithmeticException: 

Not Eligible for voting

at Example1.checkAge(Example1.java:4)

at Example1.main(Example1.java:10)


Throws Example

To understand this example you should know what is throws clause and how it is used in method declaration for exception handling, refer this guide: throws in java.

public class Example1{  

   int division(int a, int b) throws ArithmeticException{  

int t = a/b;

return t;

   }  

   public static void main(String args[]){  

Example1 obj = new Example1();

try{

   System.out.println(obj.division(15,0));  

}

catch(ArithmeticException e){

   System.out.printIn("You shouldn't divide number by zero");

}

   }  

}

Output:

You shouldn't divide number by zero

                     OR,

Key difference between Throws and Throw in Java 

  • The basic difference between these two terms is that ‘throws’ keyword uses the name of the exception classes where the ‘throw’ keyword uses the exception object.
  • The ‘throw’ keyword can throw only one i.e. a single exception instance. On the other hand, throws keyword can throw multiple exception classes and separate by a comma.
  • The ‘throw’ keyword is used to simply throw an exception where ‘throws’ keyword is used for declaration of exception, which indicates the exception that is thrown by the method.
  • The ‘throw’ keyword could be utilised inside method or static block initializer. The ‘throws,’ on the other hand, could only be used in method declaration.
  • The ‘throw’ keyword is unable to propagate the unchecked exception to the calling method where ‘throws’ keyword is used to propagate exception to the calling method. However, unchecked exception could be propagated by using throw keyword word.
  • Another basis for the difference between the two is syntax. The syntax of ‘throw’ is followed by an instance variable but syntax of ‘throws’ is followed by the exception class names.
  • ’Throw’ keyword is used within the method where ‘throws’ keyword is used with the method signature.

Example of throw vs throws
Example of throw
JavaTester.java

public class JavaTester{
   public void checkAge(int age){
      if(age<18)
         throw new ArithmeticException("Not Eligible for voting");
      else
         System.out.println("Eligible for voting");
   }
   public static void main(String args[]){
      JavaTester obj = new JavaTester();
      obj.checkAge(13);
      System.out.println("End Of Program");
   }
}
Output
Exception in thread "main" java.lang.ArithmeticException:
Not Eligible for voting
at JavaTester.checkAge(JavaTester.java:4)
at JavaTester.main(JavaTester.java:10)


Example of throws
JavaTester.java

public class JavaTester{
   public int division(int a, int b) throws ArithmeticException{
      int t = a/b;
      return t;
   }
   public static void main(String args[]){
      JavaTester obj = new JavaTester();
      try{
         System.out.println(obj.division(15,0));
      }
      catch(ArithmeticException e){
         System.out.println("You shouldn't divide number by zero");
      }
   }
}
Output
You shouldn't divide number by zero

Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Explain Parallel Efficiency of MapReduce.

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?