Exception Handling: Example

View previous topic View next topic Go down

Exception Handling: Example

Post by BIT0112-Rokon on Thu Oct 29, 2009 5:15 am

Look the example carefully--
Code:
import import java.util.InputMismatchException;
import java.util.Scanner;

  public class DivideByZeroNoExceptionHandling{
               // demonstrates throwing an exception when a divide-by-zero occurs
             public static int quotient( int numerator, int denominator )throws ArithmeticException{
                               return numerator / denominator; // possible division by zero
                             } 
              public static void main( String args[] ){
                        Scanner scanner = new Scanner( System.in ); // scanner for input
                        boolean continueLoop = true; // determines if more input is needed
                        do {                                                                   
                                 try {// read two numbers and calculate quotient                                                                 
                                          System.out.print( "Please enter an integer numerator:" );   
                                          int numerator = scanner.nextInt();                           
                                          System.out.print( "Please enter an integer denominator: " );   
                                          int denominator = scanner.nextInt();                                                                                                   
                                          int result = quotient( numerator, denominator );             
                                          System.out.printf( "\nResult: %d / %d = %d\n", numerator,denominator, result );                                     
                                         continueLoop = false; // input successful; end looping       
                                       }catch ( InputMismatchException inputMismatchException ){                                                               
                                                 System.err.printf( "\nException: %s\n",inputMismatchException );                                 
                                                 scanner.nextLine(); // discard input so user can try again   
                                                 System.out.println("You must enter integers. Please try again.\n" );         
                                             } catch ( ArithmeticException arithmeticException ){                                                               
                                                 System.err.printf( "\nException: %s\n", arithmeticException );
                                                 System.out.println("Zero is an invalid denominator. Please try again.\n" );   
                                              } // end catch                                                   
                                  } while ( continueLoop ); // end do...while                         
                       } 
         } 

output: 
Please enter an integer numerator: 45
Please enter an integer denominator: 2

Result: 45 / 2 = 22

Please enter an integer numerator: 45
Please enter an integer denominator: 0

Exception: java.lang.ArithmeticException: / by zero
Zero is an invalid denominator. Please try again.
 
Please enter an integer numerator:45
Please enter an integer denominator: 23.23

Exception: java.util.InputMismatchException
You must enter integers. Please try again.

Please enter an integer numerator: 45
Please enter an integer denominator: rokon
Exception: java.util.InputMismatchException
You must enter integers. Please try again.

Please enter an integer numerator:

avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Exception Handling: Example

Post by BIT0122-Amit on Thu Oct 29, 2009 11:50 pm

Dude... Keep it up Smile

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Exception Handling: Example

Post by BIT0129-Tabassum on Fri Oct 30, 2009 3:23 pm

Thank you.
Hoping it will be very helpful for our upcoming java exam...
avatar
BIT0129-Tabassum
Global Moderator
Global Moderator

Course(s) :
  • BIT

Blood Group : A+
Posts : 1496
Points : 2298

View user profile http://probe-tabassum.blogspot.com

Back to top Go down

Re: Exception Handling: Example

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum