Saturday, August 4, 2007

code: Zero Division

***Exception Handling***

This first program is a simple one that shows what happens when you divide by zero or if you enter a letter instead of a number when you try to divide two numbers. The first sample does not have any type of exception handling... When you try and run this program make sure that you enter a zero for the second number so that you get the exception shown (you can also enter a letter when it's asking you for a number and you will get a inputMismatchException). As you can see it can get really messy and unprofessional.

/**
* Author: http://javacodee.blogspot.com/
* Date: Aug 5, 2007
* Time: 12:49:42 AM
*/
import java.util.Scanner;

public class DivisionByZeroNoExceptionHandling
{
public static void main(String args[])
{
Scanner input =new Scanner(System.in);
int topNumber =0;
int bottomNumber =0;

System.out.print("Enter the first number: ");
topNumber =input.nextInt();
System.out.print("Enter the second number: ");
bottomNumber =input.nextInt();

System.out.println("\n****************************\nRESULT:\n" +topNumber +" divided by " +bottomNumber
+" equals " +result(topNumber, bottomNumber) +"\n****************************");
}

public static int result(int numerator, int denominator)
{
return numerator /denominator;
}
}

This next code sample will be using exception handling and you should be able to see the difference it makes just make sure you enter wrong input just like in the first example. Enter a zero for the second number and/or enter a letter for any of the numbers.

Exception handling is useful because it removes the error-handling code from the program and it improves clarity of the program. When you write a exception-prone program you can decide to handle any or all exceptions or all exceptions of a certain type.

/**
* Author: http://javacodee.blogspot.com/
* Date: Aug 4, 2007
* Time: 11:02:04 AM
*/
import java.util.Scanner;
import java.util.InputMismatchException;

public class DivisionByZero
{
public static void main(String args[])
{
Scanner input =new Scanner(System.in);
int topNumber =0;
int bottomNumber =0;
boolean loopAgain =true;

do
{
try
{
System.out.print("Enter the first number: ");
topNumber =input.nextInt();
System.out.print("Enter the second number: ");
bottomNumber =input.nextInt();

System.out.println("\n****************************" +
"\nRESULT:\n" +topNumber +" divided by " +bottomNumber +" equals " +
result(topNumber, bottomNumber) +"\n****************************");
loopAgain =false;
}
catch(ArithmeticException arithmeticEx)
{
System.out.println("\n***Exception***\n***Zero is invalid denominator***\n");
}
catch (InputMismatchException inputMismatchEx)
{
System.out.println("\n***Exception***\n***Please enter numbers only***\n");
input.nextLine();
}
}
while(loopAgain);
}

public static int result(int numerator, int denominator)
{
return numerator /denominator;
}
}

1 comment:

Eiti Kimura said...

Hello. It's a nice program I'm looking for ways to put java code on blogger. How did you format code like this? Do you have any tip?

Thanks,