// DivideByZeroExceptionTester2.java
// Demo with the try-catch code
// APPLET CODE="DivideByZeroExceptionTester2" WIDTH=750 HEIGHT=400
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DivideByZeroExceptionTester2 extends JApplet {
	Container c;
	JLabel prompt1, prompt2, prompt3;
	JTextField input1, input2, output;
	int n, d;
	double q;

	public void init( ){
		c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		prompt1 = new JLabel( "Enter the numerator:  " );
		prompt2 = new JLabel( "Enter the denominator:  " );
		prompt3 = new JLabel( "Quotient: " );
		input1 = new JTextField( 10 );
		input2 = new JTextField( 10 );
		input2.addActionListener(
			new ActionListener( ) {
				public void actionPerformed( ActionEvent e ) {
					n = Integer.parseInt( input1.getText( ) );
					d = Integer.parseInt( input2.getText( ) );
			
					try {
						if ( d == 0 )
							throw new DivideByZeroException( );
						q = ( double ) n / d;
						output.setText( Double.toString( q ) );
					}
					catch ( DivideByZeroException ex ) {
						// change d to a very large number and proceed
						d = 9999999;
						q = ( double ) n / d;
						output.setText( Double.toString( q ) );
					}
					catch ( ArithmeticException except ) {
						output.setText( except.toString( ) );
					}
					catch( Exception exp ) {
						output.setText( exp.toString( ) );
					}
				}
			}
		);
					
		output = new JTextField( 60 );

		c.add( prompt1 );
		c.add( input1 );
		c.add( prompt2 );
		c.add( input2 );
		c.add( prompt3 );
		c.add( output );
	}
}