ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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.

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!