当前位置: 技术问答>java相关
一个异常处理的问题
来源: 互联网 发布时间:2015-10-11
本文导语: 自定义一个异常类 public class NumberRangeException extends Exception { public NumberRangeException(String msg) { super(msg); } } 然后再写一个applet import java.awt.*; import java.awt.event.*; import java.applet.*; public class Ex...
自定义一个异常类
public class NumberRangeException extends Exception
{
public NumberRangeException(String msg)
{
super(msg);
}
}
然后再写一个applet
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ExceptionApplet4 extends Applet
{
TextField textField1, textField2;
String answerStr;
public void init()
{
this.resize(400,300);
textField1 = new TextField(15);
add(textField1);
textField2 = new TextField(15);
add(textField2);
answerStr = "Undefined";
//create an inner ActionListener class
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
performAction();
}
};
textField1.addActionListener(listener);
textField2.addActionListener(listener);
}
public void paint(Graphics g)
{
Font font = new Font("TimesRoman", Font.PLAIN, 24);
g.setFont(font);
g.drawString("The answer is:", 50, 100);
g.drawString(answerStr, 70, 130);
}
public void performAction() throws NumberRangeException
{
String str1 = textField1.getText();
String str2 = textField2.getText();
try{
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
if ((int120)||(int220))
{
NumberRangeException e = new NumberRangeException("Numbers no within the specified range");
throw e;
}
int answer = int1 / int2;
answerStr = String.valueOf(answer);
repaint();
}catch(NumberFormatException e)
{
e.getMessage();
}
}
}
编译出错,
unreported exception NumberRangeException :must be caught or declared to be throw
performAction()
什么问题啊,,我的自定义类编译通过了啊;怎么找不到呢
public class NumberRangeException extends Exception
{
public NumberRangeException(String msg)
{
super(msg);
}
}
然后再写一个applet
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ExceptionApplet4 extends Applet
{
TextField textField1, textField2;
String answerStr;
public void init()
{
this.resize(400,300);
textField1 = new TextField(15);
add(textField1);
textField2 = new TextField(15);
add(textField2);
answerStr = "Undefined";
//create an inner ActionListener class
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
performAction();
}
};
textField1.addActionListener(listener);
textField2.addActionListener(listener);
}
public void paint(Graphics g)
{
Font font = new Font("TimesRoman", Font.PLAIN, 24);
g.setFont(font);
g.drawString("The answer is:", 50, 100);
g.drawString(answerStr, 70, 130);
}
public void performAction() throws NumberRangeException
{
String str1 = textField1.getText();
String str2 = textField2.getText();
try{
int int1 = Integer.parseInt(str1);
int int2 = Integer.parseInt(str2);
if ((int120)||(int220))
{
NumberRangeException e = new NumberRangeException("Numbers no within the specified range");
throw e;
}
int answer = int1 / int2;
answerStr = String.valueOf(answer);
repaint();
}catch(NumberFormatException e)
{
e.getMessage();
}
}
}
编译出错,
unreported exception NumberRangeException :must be caught or declared to be throw
performAction()
什么问题啊,,我的自定义类编译通过了啊;怎么找不到呢
|
你没有catch 你的Exception呀!!
加上catch(NumberRangeException e){}试试
加上catch(NumberRangeException e){}试试
|
1:
public void actionPerformed(ActionEvent evt)
{
performAction();
}
2:
public void performAction() throws NumberRangeException
请比较1与2,一个方法定义时抛出异常在引用时也要处理
public void actionPerformed(ActionEvent evt)
{
performAction();
}
2:
public void performAction() throws NumberRangeException
请比较1与2,一个方法定义时抛出异常在引用时也要处理
|
因为你的performAction()方法throws NumberRangeException
但是你在调用他的时候没有catch这个异常;
但是你在调用他的时候没有catch这个异常;