catchingexceptions.java
来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 39 行
JAVA
39 行
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 + =
减小字号Ctrl + -
显示快捷键?