⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catchingexceptions.java

📁 《A first book of java》by Gary J.Bronson 北大出版社
💻 JAVA
字号:
import javax.swing.*;
public class CatchingExceptions
{
  public static void main (String[] args) 
  {
    String s1;
    String s2;
    double num1, num2, average;
    try
    {
      s1 = JOptionPane.showInputDialog("Enter a number:");
      num1 = Double.parseDouble(s1);                
      s2 = JOptionPane.showInputDialog("Great! Now enter another number:");      
      num2 = Double.parseDouble(s2);
      average = (num1 + num2)/2.0;
   
      JOptionPane.showMessageDialog(null, 
              "The average of " + num1 + " and " + num2 + " is " + average,
             "Program 3-15",
              JOptionPane.INFORMATION_MESSAGE);
    }
    catch(NumberFormatException n)
    {
     JOptionPane.showMessageDialog(null, 
             "You must enter a number",
            "Input Data Error",
             JOptionPane.ERROR_MESSAGE);
    }    
    catch(NullPointerException n)
    {
     JOptionPane.showMessageDialog(null, 
             "You Pressed the Cancel Button",
            "Program Termination",
             JOptionPane.ERROR_MESSAGE);
    }    
    finally{System.exit(0);}
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -