异常
作者:互联网
异常
package error;
public class Test {
public static void main(String[] args) {
int a=1,b=0;
try {
System.out.println(a/b);
} catch (Exception e) {
System.out.println("除数为0");
} catch(Throwable e){
System.out.println("出错");
}
finally {
System.out.println("善后");
}
}
}
自定义异常
package error;
public class Test2 {
static void demo(int a) throws MyException{
System.out.println("传递的数为:"+a);
if (a>10){
throw new MyException(a);
}
}
public static void main(String[] args) {
try {
demo(11);
} catch (MyException e) {
System.out.println("123\t"+e);
} finally {
}
}
}
package error;
public class MyException extends Exception{
private int a;
public MyException(int a){
this.a=a;
}
@Override
public String toString() {
return "MyException{" +
"a=" + a +
'}';
}
}
标签:int,MyException,System,println,异常,public,out 来源: https://www.cnblogs.com/Gc-mosi/p/15382844.html