Skip to content

Creating you own exception in java

September 12, 2013

Objective:

–> create your own exception 

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

JAVA CODE

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

public class Ex6
{

	// for the accepting of the input 
	public static Scanner console = new Scanner(System.in);
	// 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)
		{
			try
			{
				evaluate();

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

			}

		}
		// finally cnt is not zero
		System.out.println("DONE!!!");

	}
	// 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();
		System.out.println("*****************");
	}
}

JAVA CODE

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

Simple, Free Image and File Hosting at MediaFire

DOWNLOAD COMPLETE CODES HERE

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

Advertisements
Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

TurtleAndRobot.com

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.

paritoshmishra

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.

ProgramThat;

A simple guide to programming...

ITechonology

Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans

Gigaom

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

HarsH ReaLiTy

A Good Blog is Hard to Find

VentureBeat

News About Tech, Money and Innovation

retireediary

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

%d bloggers like this: