Java–异常机制
package com.zy.base;
import java.util.logging.Logger;
public class JavaException {
public static void main(String[] args) {
int a=1;
int b=0;
try {
new JavaException().test(a,b);
System.out.println("======================");
if(b == 0){
throw new ArithmeticException();
}
System.out.println("======================");
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
System.out.println("======================");
try {
new JavaException().a();
}catch (Exception e){
System.out.println("Exception");
}catch (Error error){
System.out.println("Error");
}catch (Throwable throwable){
System.out.println("Throwable");
}finally {
System.out.println("finally");
}
System.out.println("======================");
try {
new JavaException().userDefinedFunction(11);
} catch (UserDefinedException e) {
Logger logger = Logger.getLogger("JavaException");
logger.warning("UserDefinedException==>"+e);
}
}
public void a(){
b();
}
public void b(){
a();
}
public void test(int a,int b) throws ArithmeticException{
System.out.println(a/b);
}
public void userDefinedFunction(int number) throws UserDefinedException {
System.out.println("自定义异常测试传递的数字为:"+number);
if(number>10){
throw new UserDefinedException(number);
}
System.out.println("自定义异常测试方法正常运行");
}
}
class UserDefinedException extends Exception{
private int number;
public UserDefinedException(int number) {
this.number = number;
}
@Override
public String toString() {
return "UserDefinedException{" +
"number=" + number +
'}';
}
}