Throw Keyword in JAVA (User Defined Exception)

0
20

This tutorial is all about Throw Keyword in JAVA (User Defined Exception)
In this article we will discuss about “throw” keyword in Java. Usage of throw keyword is anti to exception handling that is we are raising exception and not handling it.

Let us take an example to understand this better.

  1. import java.util.Scanner;
  2.  
  3. public class Manager{
  4.  
  5. public static void main(String args[]) {
  6.  
  7. Scanner sc=new Scanner(System.in);
  8.  
  9. System.out.println("Enter age");
  10.  
  11. int age=sc.nextInt();
  12.  
  13. if (age <= 0) {
  14.  
  15. throw new Arithmetic Exception () ;
  16.  
  17. }
  18.  
  19. System.out.println ("continue" );
  20.  
  21. }
  22.  
  23. }

As JRE and compiler doesn’t distinguish zero, positive and negative integers. So in any case if someone enter age as zero or negative value , it is violating business logic. In order to terminate the program while such conditions occurs , we are guiding JRE not to execute the program as the condition is not suitable from business perspective.

 

Readers might read also:

 

 

 

Facebook Comments
(Visited 22 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here