我想执行throw 里的异常处理,怎么没有执行,显示结果是
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Int.main(Int.java:9)
我对异常进行了处理啊,怎么没有效果呢,盼回复,谢谢~
代码如下:Int.java
public class Int {
public static void main(String[] args) throws MissException
{ int i=10;
int j=10;
if(i==j/0){
throw new MissException();
}
System.out.println("总得执行");
}
}MissException .javapublic class MissException extends Exception {
public MissException(){
System.out .println("有错误了");
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Int.main(Int.java:9)
我对异常进行了处理啊,怎么没有效果呢,盼回复,谢谢~
代码如下:Int.java
public class Int {
public static void main(String[] args) throws MissException
{ int i=10;
int j=10;
if(i==j/0){
throw new MissException();
}
System.out.println("总得执行");
}
}MissException .javapublic class MissException extends Exception {
public MissException(){
System.out .println("有错误了");
}
}
自己定义一个异常类,写一个方法在Int中,try {....
} catch(MissException e)
{
e.定义的方法;
}类名还起成Int 不太好
自己定义一个异常类,写一个方法在Int中,try {....
} catch(MissException e)
{
e.定义的方法;
}类名还起成Int 不太好
try{
int i=10;
int j=10;
if(i==j/0){
throw new MissException();
}catch(Exception e){
System.out.println("Oh!Yeah!总算抓到了");
}
finally{
System.out.println("总得执行");
}
}
public class Test {
public static void main(String[] args) { int i=10;
int j=10;
try{
i = j/0;
}catch(ArithmeticException e){
System.out.println("出错了");
}
}
}
这样不就可以了啊,不要用throws
尽量不要对main方法用throws