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();
}
}
public void doException() throws IOException {
System.out.println("arithmeticExcption");
} public static void main(String[] args) {
Test5 test = new Test5();
test.doException();
}
}
解决方案 »
- 静态环境下不允许类的参数是泛型类型,实在不理解!
- 谁有<JAVA编程思想>最新版的电子书?
- char 型异或后乱码怎么解决?
- 日期函数格式linux/windows求解~~~
- java的类太多了,学起来真难啊.
- 我在一个COMBOBOX触发事件后,改变别一个COMBOBOX ,为什么总是不行,我简写了一个原码如下:
- 如何重命名树的节点?
- 我在JFrame窗体里加入了多个自定义的轻量组件(从Component派生),当我把它们叠加在一起的时候,只能看到最先加入的那个组件,请问有什
- 在DOS下运行java mynews出现......?
- 如何用JDBC连接SQL Server 2000的本地数据库?
- 类型转换问题
- jvm运行机制
改成:
public void doException() {
代码
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){
}
}
}
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();
}
}
}
doException本身throw了异常,在main中必须try-catch一下,或者把main声明一下throw 一个exception
/**
*
* @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();//这样就不需在调用处捕获异常了
}
}
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();
}
}
public void doException() throws ArithmeticException { System.out.println("arithmeticExcption");
} public static void main(String[] args){
Test5 test = new Test5();
test.doException();//是否可以通过编译
}
}
谢谢,这两种都是可行的,但用ArithmeticException不用IOException也可以通过编译,为什么呢