📄 catchingexceptions.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 + -