2 Replies - 2152 Views - Last Post: 26 November 2011 - 04:24 AM

#1 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Write Your Own Exceptions

Posted 05 May 2006 - 08:50 AM

Description: This example uses a divide by zero Exception. including a tester class to demonstrate the throwing.extending class Exception to generate your own output messages
Exception Class DivideByZeroException
[code]
public class DivideByZeroException extends ArithmeticException {
    public DivideByZeroException() {
          super("Attempted to divide by zero!");
    }
}

Tester Class ExceptionTester
public class ExceptionTester {
     private static int quotient(int numerator, int denominator) throws DivideByZeroException {
          if(denominator == 0)
               throw new DivideByZeroException();
          return(numerator / denominator);
     }

     public static void main(String args[]) {
          int number1=0, number2=0, result=0;
          try {
             number1 = 1;
              number2 = 0;
             result = quotient(number1,number2);
               System.out.print(number2 + " goes into " + number1);
               System.out.println(" this many times: " + result);
          } 
          catch (Exception e) {
             System.out.println(e.toString());
             System.out.println("An Exception occured");
             System.exit(-1);
          }
     }
}

[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: Write Your Own Exceptions

#2 Suffiyanish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-November 11

Re: Write Your Own Exceptions

Posted 26 November 2011 - 04:24 AM

i wonna know how to make runtimeexception.....
Was This Post Helpful? 0
  • +
  • -

#3 Suffiyanish   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-November 11

Re: Write Your Own Exceptions

Posted 26 November 2011 - 04:24 AM

i wonna know how to make runtimeexception.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1