public class auctiontest
{
private double initprice = 30.0;
// 因为该方法中显式抛出了auctionexception异常,
// 所以此处需要声明抛出auctionexception异常
public void bid(string bidprice)
throws auctionexception
{
double d = 0.0;
try
{
d = double.parsedouble(bidprice);
}
catch (exception e)
{
// 此处完成本方法中可以对异常执行的修复处理,
// 此处仅仅是在控制台打印异常跟踪栈信息。
e.printstacktrace();
// 再次抛出自定义异常
throw new auctionexception("竞拍价必须是数值,"
+ "不能包含其他字符!");
}
if (initprice > d)
{
throw new auctionexception("竞拍价比起拍价低,"
+ "不允许竞拍!");
}
initprice = d;
}
public static void main(string[] args)
{
auctiontest at = new auctiontest();
try
{
at.bid("df");
}
catch (auctionexception ae)
{
// 再次捕捉到bid方法中的异常。并对该异常进行处理
system.err.println(ae.getmessage());
}
}
}
auctionexception.java
public class auctionexception extends exception
{
// 无参数的构造器
public auctionexception(){} //①
// 带一个字符串参数的构造器
public auctionexception(string msg) //②
{
super(msg);
}
}
2 运行结果
java.lang.numberformatexception: for input string: "df"
at sun.misc.floatingdecimal.readjavaformatstring(floatingdecimal.java:1224)
at java.lang.double.parsedouble(double.java:510)
at auctiontest.bid(auctiontest.java:16)
at auctiontest.main(auctiontest.java:39)
package com.exception.demo;
public class main {
public static void main(string[] args) {
main main = new main();
}
public void methodtry() {
}
public void methodthrow() {
}
}
初始环境就是这么简答。 2:下面给方法methodtry加上方法主体:
public static void main(string[] args) {
main main = new main();
main.methodtry();
}
public void methodtry() {
int a=10;
int b=0;
int c=a/b;
}
public static void main(string[] args) throws exception {
main main = new main();
main.methodthrow();
}
public void methodthrow() throws exception {
throw new exception("这里有异常");
}