12实验十二 异常处理实验代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// read two numbers and calculate quotient try { dayNumber = Integer.parseInt( dayNumberField.getText() ); dayName = getDayName(dayNumber);
代码5 // 在控件dayNameField中输出结果 } catch (BadDataException ex) {
// get content pane and set its layout Container container = getContentPane(); container.setLayout( new GridLayout( 2 , 2 ) );
// set up label and numeratorField container.add(
numeratorField = new JTextField( 10 ); container.add( numeratorField );
container.add( new JLabel( "Enter denominator and press Enter ", SwingConstants.RIGHT ) );
代码4 // 设置窗口的大小为 (425,100) setVisible( true ); } // end Constructor DivideByZeroGUI
// process GUI events public void actionPerformed( ActionEvent event ) { dayNameField.setText( "" ); // clear outputField
程序清单12-2
import java.awt.*; import java.awt.event.*; import javax.swing.*;
// Container, GridLayout // ActionListener // JFrame
public class DivideByZeroGUI extends JFrame implements ActionListener
try { System.out.println("Method main called ");
methodA(); } catch(MyException ex) { System.out.println("Exception caught in Main "); } } // end main
}
程序清单12-4
class MyException extends Throwable { public MyException() {
System.out.println("Exception thrown in MyException "); } }
public class SearchForExceptionHandler { static public void main(String[] args) {
程序清单12-3 import java.awt.*; import java.awt.event.*; import javax.swing.*;
// Container, GridLayout // ActionListener
public class BadDataDays extends JFrame implements ActionListener
// set up label and denominatorField; register listener container.add(
new JLabel( "星期几 ", SwingConstants.RIGHT ) ); dayNameField = new JTextField( 10 ); 代码3 // 将控件dayNameField 加入到容器中
outputField = new JTextField(); container.add( outputField );
setSize( 425, 100 ); setVisible( true ); } public void actionPerformed( ActionEvent event ) { outputField.setText( "" ); // 读入两个数并计算商 numerator = Integer.parseInt( numeratorField.getText() ); denominator= Integer.parseInt( denominatorField.getText() );
JOptionPane.showMessageDialog( BadDataDays.this, ex.toString(), "无效日期", JOptionPane.WARNING_MESSAGE ); } catch (NumberFormatException ex) { System.out.println("I detected Exception " + ex.toString());
static void methodA() throws MyException { System.out.println("Method A called "); methodB(); }
static void methodB() throws MyException { System.out.println("Method B called "); methodC(); }
{ private JTextField numeratorField, denominatorField, outputField; private int denominator, numerator, quotient; public DivideByZeroGUI()
{ super( "Divide ints by zero - no exception handling" );
} // end quotient
public static void main( String args[] ) { BadDataDays application = new BadDataDays(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // end main
Container container = getContentPane(); container.setLayout( new GridLayout( 3, 2 ) );
container.add( new JLabel( "Enter numerator ", SwingConstants.RIGHT ) );
denominatorField = new JTextField( 10 ); container.add( denominatorField ); denominatorField.addActionListener( this );
container.add( new JLabel( "RESULT ", SwingConstants.RIGHT ) );
{ private JTextField dayNumberField, dayNameField; private int dayNumber; private String dayName;
// set up GUI public BadDataDays() { super( " 代码1 " );
实验十二 异常处理
程序清单 12-1 // DivideByZero.java public claห้องสมุดไป่ตู้s DivideByZero { private int denominator, numerator, quotient; public DivideByZero() {
denominator = 3; numerator = 12; quotient = quotient( numerator, denominator );
System.out.print("Quotient is " + quotient); } public int quotient ( int numerator, int denominator ) { return numerator / denominator; } public static void main( String args[] ) { DivideByZero application = new DivideByZero(); } }
quotient = quotient( numerator, denominator ); outputField.setText( Integer.toString(quotient) );
}
public int quotient( int numerator, int denominator ) { return numerator / denominator; } public static void main( String args[] ) { DivideByZeroGUI application = new DivideByZeroGUI(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
static void methodC() throws MyException { System.out.println("Method C called "); throw new MyException(); } } // end SearchForExceptionHandler
} // end catch
} // end actionPerformed
public String getDayName(int dayNumber) throws BadDataException {
代码6 // 此处加入switch 语句块以根据输入值dayNumber 判断星期几,并 在default处Throw抛出BadDataException异常
new JLabel( "输入数字 ", SwingConstants.RIGHT ) );
代码2 // 创建一个JTextField控件 dayNumberField container.add( dayNumberField ); dayNumberField.addActionListener( this );
相关文档
最新文档