então ai vai um exemplo de exceção lançada por divisão por zero:
[code]public class DivideByZeroException extends ArithmeticException {
public DivideByZeroException()
{
super( "Attempted to divide by zero" );
}
public DivideByZeroException( String msg )
{
super( msg );
}
}[/code]
…eai vai o programinha pra tu lançá-la:
[code]import java.awt.;
import java.awt.event.;
import java.text.;
import javax.swing.;
public class DivideByZeroTest extends JFrame implements ActionListener {
private JTextField input1, input2, output;
private int n1, n2;
private double result;
public DivideByZeroTest()
{
super( "Demonstrando excecoes" );
getContentPane().setLayout( new GridLayout( 3, 2 ) );
getContentPane().add( new JLabel( "Numerador: ", SwingConstants.RIGHT ) );
input1 = new JTextField( 10 );
getContentPane().add( input1 );
getContentPane().add( new JLabel( "Denominador: ", SwingConstants.RIGHT ) );
input2 = new JTextField( 10 );
getContentPane().add( input2 );
input2.addActionListener( this );
getContentPane().add( new JLabel( "Resultado: ", SwingConstants.RIGHT ) );
output = new JTextField();
output.setEditable( false );
getContentPane().add( output );
setSize( 425, 100 );
show();
}
public void actionPerformed( ActionEvent e )
{
DecimalFormat precision3 = new DecimalFormat( "0.000" );
output.setText( "" );
try {
n1 = Integer.parseInt( input1.getText() );
n2 = Integer.parseInt( input2.getText() );
result = quociente( n1, n2 );
output.setText( precision3.format( result ) );
} catch ( NumberFormatException numberFormat ) {
JOptionPane.showMessageDialog( this, "Digite 2 inteiros neh" );
} catch ( ArithmeticException ex ) {
JOptionPane.showMessageDialog( this, ex.toString() );
}
}
private double quociente( int n1, int n2 ) throws DivideByZeroException
{
if ( n2 == 0 )
throw new DivideByZeroException();
return ( double ) n1 / n2 ;
}
public static void main( String args[] )
{
DivideByZeroTest win = new DivideByZeroTest();
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}[/code]