例外處理- Exception ( try catch )
- try{
- String strInput = jTextField1.getText();
- double height = Double.parseDouble(strInput);
- String strInput2 = jTextField2.getText();
- double weight = Double.parseDouble(strInput2);
- double BMI = weight / (height*height);
- jLabel2.setText("您的BMI值為: " + BMI);
- }
- catch(Exception ex)
- {
- jLabel2.setText("發生無預期錯誤:" + ex.toString());
- }
複製代碼 |