问题描述:
import java.awt.*
import java.awt.event.*
import javax.swing.*
自定义异常类
class NumException extends Exception{
public String toString(){
return "输入的值太小"
}
public String shuru(){
return "请输入数字"
}
}
class jisuanqi extends JFrame implements ActionListener,ItemListener{
JRadioButton rz
JRadioButton rm
ButtonGroup bg
JTextField txt
JButton btnj
String msg
jisuanqi(){
super("计算器")
rz=new JRadioButton("周长")
rm=new JRadioButton("面积")
bg=new ButtonGroup()
txt=new JTextField()
btnj=new JButton("计算")
msg=""
this.getContentPane().setLayout(new GridLayout(2,2))
bg.add(rz)
bg.add(rm)
this.getContentPane().add(rz)
this.getContentPane().add(rm)
this.getContentPane().add(txt)
this.getContentPane().add(btnj)
this.setSize(200,200)
rz.addItemListener(this)
rm.addItemListener(this)
btnj.addActionListener(this)
}
实现接口
public void actionPerformed(ActionEvent e) throws NumException{声明异常
if (Double.parseDouble(txt.getText())<=0){
throw new NumException()抛出异常
}
if (msg.equals("周长")){
txt.setText(""+(3.14*2*Double.parseDouble(txt.getText())))
}
if (msg.equals("面积")){
txt.setText(""+3.14*(Double.parseDouble(txt.getText())*Double.parseDouble(txt.getText())))
}
}
实现接口
public void itemStateChanged(ItemEvent e){
if (e.getSource()==rz){
msg="周长"
}
if (e.getSource()==rm){
msg="面积"
}
}
public static void main(String args[]){
try{
jisuanqi js=new jisuanqi()
js.setVisible(true)
}
catch(NumException ex){
System.out.println(ex)
}
}
}
编译时出现:
C:\Documents and Settings\Administrator\桌面\新建文件夹\jisaunqi.java:45: jisuanqi 中的 actionPerformed(java.awt.event.ActionEvent) 无法实现 java.awt.event.ActionListener 中的 actionPerformed(java.awt.event.ActionEvent);被覆盖的方法不抛出 NumException
解析:
这段代码有问题:
实现接口
public void actionPerformed(ActionEvent e) throws NumException{声明异常
public void actionPerformed(ActionEvent e)不应该抛出异常, ActionListener接口函数没有异常抛出
一种可能的解决方法是把判断移到另外一个函数中,然后抛出异常,在actionPerformed中捕获或者在外面捕获
class ParameterNumberException extends Exception{ParameterNumberException(){
super("参数个数异常!")}
public String toString(){
return "ParameterNumberException"}
}
class ParameterFormatException extends Exception{
ParameterFormatException(){
super("参数格式异常!")
public String toString(){
return "ParameterFomatException"}
}
public class NullPointer2 {
public static void main(String args[])throws ParameterNumberException,ParameterFormatException{
try{
if(args.length<2)
throw new ParameterNumberException()
else
throw new ParameterFormat()
}
catch(ParameterNumberException e){
System.out.println(e)
e.printStackTrace()}
catch(ParameterFomatException e){
System.out.println(e)
e.printStackTrace()}
}
}
runtimeException是Exception的一个子类型,自定义异常可以继承Exception或其子类.通常调用一下父类的构造方法。
public class MyException extends Exception {
public MyException() {super() }
public MyException(String msg) {super(msg) }
public MyException(Throwable cause) { super(cause) }
public MyException(String msg, Throwable cause) { super(msg, cause) }
}
public String[] createArray(int length) throws MyException {
if (length <0) {
throw new MyException("数组长度小于0,不合法")
}
return new String[length]
}
上面这个例子,你继承RuntimeException也是没问题的.通常继承Exception,范围更大.