Skip to content

Creating you own exception in java

September 12, 2013


–> create your own exception 

–> continue to accept input until the right input is placed


// import the packages
import PackageException.NegativeException;
import java.util.*;

public class Ex6

	// for the accepting of the input 
	public static Scanner console = new Scanner(;
	// a counter to countinue to accept input until
	// no error is detected in your input
	public static int cnt = 0;
	// where the fun begins
	public static void main(String[] args)

		// loops until the cnt is not zero, 
		// that is, if there's no error,
		// cnt i will turn to 1
		while(cnt == 0)

			// a user-defined Exception, see the 
			// code in this file 
			catch(NegativeException e)
				// outputs the message about the error
				// in which is overriden of the class NegativeException
				// ensure to continue asking for input
				cnt = 0;


		// finally cnt is not zero

	// throws meand ignore, in this method and the error will
	// be felt back where the function is called
	public static void evaluate() throws NegativeException
		double x, y;
		System.out.print("ENTER TWO non negative NUMBERS: ");
		x = console.nextDouble();
		y = console.nextDouble();
		cnt = 1;
		// if the two vars arent postive
		if(x < 0 || y < 0)
			// throws the exceptions
			throw new NegativeException();


package PackageException;public class NegativeException extends Exception
	public NegativeException()
		super("negative numbers are not allowed you dimwit");
	public NegativeException(String msg)

Simple, Free Image and File Hosting at MediaFire


although if you input something lik chars or string, the program will terminate, its just to show how user-defined exception works

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.


Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.


A simple guide to programming...


Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans


Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media


News About Tech, Money and Innovation


The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

Waterlooville Designs

A Professional Graphic Designer In Waterlooville.

%d bloggers like this: