为什么不能通过编译,求指教?

yllzhq 2010-09-24 07:50:51
import java.io.*;

public class Test5 {
public void doException() throws IOException {
System.out.println("arithmeticExcption");
}

public static void main(String[] args) {
Test5 test = new Test5();

test.doException();
}
}
...全文
221 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
steely_chen 2010-09-26
  • 打赏
  • 举报
回复
楼上的和大虾们都解释过了,我是来赚积分的
yllzhq 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 leepenghom 的回复:]
两种方法。一种是主函数继续往外抛异常。一种是上面讲的一样捕捉异常。但是应该捕捉!
import java.io.*;

public class Test5 {
public void doException() throws IOException {
System.out.println("arithmeticException");
}

public static void……
[/Quote]
偶想明白了,谢了
yllzhq 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 doingendlessly 的回复:]
3L说的对。

Java中如果定义的方法会抛出异常,那调用的时候就一定要捕捉该异常,当然如果你忘了会抛出什么异常,那直接用Exception就行了。
[/Quote]
import java.io.*;

public class Test5 {
public void doException() throws ArithmeticException { System.out.println("arithmeticExcption");
}

public static void main(String[] args){
Test5 test = new Test5();

test.doException();//是否可以通过编译
}
}
谢谢,这两种都是可行的,但用ArithmeticException不用IOException也可以通过编译,为什么呢
leepenghom 2010-09-26
  • 打赏
  • 举报
回复
两种方法。一种是主函数继续往外抛异常。一种是上面讲的一样捕捉异常。但是应该捕捉!
import java.io.*;

public class Test5 {
public void doException() throws IOException {
System.out.println("arithmeticException");
}

public static void main(String args[]) throws Exception {
Test5 test = new Test5();
test.doException();
}
}

shuvsbiao 2010-09-26
  • 打赏
  • 举报
回复
哈哈 我也学了不少
SuperCodingMan 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 liukelin 的回复:]

这么简单的问题 还贴上来丢人
[/Quote]
呵呵,我不觉得发这种贴有什么好丢人的。学习是一个过程,说不准楼主很快就比你更精通java。
SuperCodingMan 2010-09-24
  • 打赏
  • 举报
回复
因为你的方法把异常抛出给函数的调用者了,所以你必须要在调用函数的地方捕获这个异常:

import java.io.*;
/**
*
* @author supercodingman
* @version 1.0
*/
public class Test5 {
public void doException() throws IOException {
System.out.println("arithmeticExcption");
}
public static void main(String[] args) {
Test5 test = new Test5();
try {
test.doException();//所抛出的异常会返回给调用者处理
} catch (IOException e) {
e.printStackTrace();
}
}
}


你也可以换成把throws的方式换成try/catch块:


package csdn.javase.excep.ex1;
import java.io.*;
/**
*
* @author supercodingman
* @version 2.0
*/
public class Test5 {
public void doException(){
try{
System.out.println("arithmeticExcption");
throw new IOException();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String[] args) {
Test5 test = new Test5();
test.doException();//这样就不需在调用处捕获异常了
}
}
eggno8 2010-09-24
  • 打赏
  • 举报
回复
有异常抛出机制的函数A,其他函数比如B调用A的时候要么try块进行处理,要么B也throws继续抛,然后让别的函数或者程序段调用B的时候来做处理。
dahaidao 2010-09-24
  • 打赏
  • 举报
回复
在这里丢人没什么吧,别在客户那里丢人就好了。
liukelin 2010-09-24
  • 打赏
  • 举报
回复
这么简单的问题 还贴上来丢人
pi88dian88 2010-09-24
  • 打赏
  • 举报
回复
调用的函数doException()里申明抛出异常,这里的main()函数里也应该抛出这个异常
whut_lcy 2010-09-24
  • 打赏
  • 举报
回复
有异常你没捕获。。。
doException本身throw了异常,在main中必须try-catch一下,或者把main声明一下throw 一个exception
doingendlessly 2010-09-24
  • 打赏
  • 举报
回复
3L说的对。

Java中如果定义的方法会抛出异常,那调用的时候就一定要捕捉该异常,当然如果你忘了会抛出什么异常,那直接用Exception就行了。
雪岢奇 2010-09-24
  • 打赏
  • 举报
回复
有提示信息的嘛,因为你定义的方法是抛出(throws IOException ),所以方法的调用寻妖抛出
package test1;

import java.io.*;

public class Test5 {
public void doException() throws IOException {
System.out.println("arithmeticExcption");
}

public static void main(String[] args) {
Test5 test = new Test5();

try {
test.doException();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
mm5201234 2010-09-24
  • 打赏
  • 举报
回复
拜读了
hanRivergo 2010-09-24
  • 打赏
  • 举报
回复
以上讲得对,好。
numb09 2010-09-24
  • 打赏
  • 举报
回复
ovecy 2010-09-24
  • 打赏
  • 举报
回复
捕获一下异常不就行了么
代码
import java.io.*;

public class Test5 {
public void doException() throws IOException {
System.out.println("arithmeticExcption");
}

public static void main(String[] args) {
try{
Test5 test = new Test5();

test.doException();
}catch(Exception e){
}
}
}
huhuyuan7 2010-09-24
  • 打赏
  • 举报
回复
捕获异常就好了,三楼的解释就很好
yekongjiliao 2010-09-24
  • 打赏
  • 举报
回复
应该捕获异常。。。
加载更多回复(4)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧