Throw Keyword in JAVA (User Defined Exception)

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.

[java]

import java.util.Scanner;

public class Manager{

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println(“Enter age”);

int age=sc.nextInt();

if (age <= 0) {

throw new Arithmetic Exception () ;

}

System.out.println (“continue” );

}

}

[/java]

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.

Itsourcecode is designed to help those newbie programmers to widen their knowledge and enhance their skills in programming through free source code from different programming languages such as VB.net, PHP, Java, HTML, CSS, etc.