异常,和log4j(老司机)
public class SampleThrow {
public static void main(String[] args) {
System.out.println("AAAAAAAAAAAAAAAAA");
try {
int num = 0;
if(num==0){
num=1;
}
num = 10 / num; // 系统自动抛出异常
// 发生的异常类型与catch中异常的类型相同则可以被捕获
// 发生的异常类型是catch中异常的类型的子类也可以被捕获
// 如果try部分的代码没有发生错误,catch块的代码不会执行
} catch (Exception e) {
System.out.println("BBBBBBBBBBBBBBBBBBBBBBBB");
e.printStackTrace();
}
}
}
/**
* 检查型异常是在编译阶段直接报错的异常,必须要处理的异常
* @author
* @date 2018年1月30日上午9:56:53
*/
public class SampleCheckException {
public static void main(String[] args) {
int num;
// ArithmeticException属于非檢査型异常,可以不用try{}catch{}
// num = test1(10, 2);
// System.out.println(num);
// num = test1(10, 0);
// System.out.println(num);
// try {
// test2(10, 1); // IllegalAccessException属于檢査型异常
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
//在catch块中处理的异常是try语句快种异常的父类或者是该异常,不能是子类
try {
test3(10, 1); // test3抛出的是Exception,catch部分不能是IllegalAccessException
} catch (Exception e) {
e.printStackTrace();
}
}
private static int test1(int num1, int num2) {
if (num2 == 0) {
// ArithmeticException繼承自RuntimeException
throw new ArithmeticException("num is 0 err1");
}
return num1 / num2;
}
private static int test2(int num1, int num2) throws IllegalAccessException {
if (num2 == 0) {
// IllegalAccessException直接繼承自Exception
throw new IllegalAccessException("num is 0 err2");
}
return num1 / num2;
}
private static int test3(int num1, int num2) throws Exception { // throws部分可以是父類
if (num2 == 0) {
// IllegalAccessException直接繼承自Exception
throw new IllegalAccessException("num is 0 err3");
}
return num1 / num2;
}
}
/**
*
* @author
* @date 2018年1月30日上午10:03:50
*/
public class SampleExceptionMethod {
public static void main(String[] args) {
try {
test(10, 0); // test3抛出的是Exception,catch部分不能是IllegalAccessException
} catch (Exception e) {
e.printStackTrace();
System.out.println("------華麗的-----------------------分割線-----");
System.out.println(e.getMessage());
System.out.println("------華麗的-----------------------分割線-----");
System.out.println(e.toString());
}
}
private static int test(int num1, int num2) throws Exception { // throws部分可以是父類
if (num2 == 0) {
// IllegalAccessException直接繼承自Exception
throw new IllegalAccessException("num is 0 这个是除数为0的异常");
}
return num1 / num2;
}
}
public class SampleFinally {
public static void main(String[] args) {
// 未使用finally,如果希望不管是否發生异常,都要打印出“over”,需要在毎个地方加上打印語句
// try {
// test(-10);
// System.out.println("OK");
// System.out.println("over");
// } catch (ArithmeticException e) {
// System.out.println("err1");
// System.out.println("over");
// } catch (Exception e) {
// System.out.println("err2");
// System.out.println("over");
// }
// System.out.println("------華麗的-----------------------分割線-----");
// 使用finally,只需要在finally块中加上打印語句即可
try {
test(10);
System.out.println("OK");
return ;
} catch (ArithmeticException e) {
System.out.println("err1");
} catch (Exception e) {
System.out.println("err2");
} finally {
System.out.println("over");
}
}
private static void test(int num) {
if (num <= 0) {
throw new ArithmeticException("num is 0");
}
}
}
public class SampleException {
private static org.apache.log4j.Logger logger1 = org.apache.log4j.Logger.getLogger("SampleException");
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("busi1");
public static void main(String[] args) {
int[] arr = { 1, 2 };
try {
arr[2] = 3;
System.out.println("OK1");
} catch (ArrayIndexOutOfBoundsException e) { // 只处理数组下标越界异常
System.out.println("ERR1");
}
System.out.println("------華麗的-----------------------分割線-----");
try {
arr[1] = 3;
System.out.println("OK2");
} catch (Exception e) { // 使用Exception表示所有的异常都使用相同的方式處理
System.out.println("ERR2");
}
SampleException se = new SampleException();
se.testLog();
}
public void testLog() {
logger1.info("默认输出路径");
logger.info("自定义输出路径");
}
}
/**
*
* @author
* @date 2018年1月30日上午10:35:08
*/
class MyException extends Exception {
private static org.apache.log4j.Logger logger1=org.apache.log4j.Logger.getLogger("MyException");
private static org.apache.log4j.Logger logger=org.apache.log4j.Logger.getLogger("busi");
private static final long serialVersionUID = -5171311572974280588L;
public MyException() {
super("MyException 出现异常errerr");
}
public MyException(String msg) {
super(msg);
}
public void testLog(){
logger1.info("默认输出路径");
logger.info("自定义输出路径");
}
}
public class SampleExceptionEx {
public static void f(int num) throws MyException {
if (num < 0) {
throw new MyException();
}
}
public static void g(int num) throws MyException {
if (num == 0) {
throw new MyException("Throwing MyException from g()");
}
}
public static void main(String[] args) {
// try {
// f(-1);
// } catch (MyException e) {
// System.out.println(e.getMessage());
// }
//
// System.out.println("------壺楉揑-----------------------暘妱慄-----");
//
// try {
// f(10);
// g(0);
// } catch (MyException e) {
// System.out.println(e.getMessage());
// e.printStackTrace();
// System.out.println(e.toString());
// }
// try {
// int arr[]=new int[2];
//// arr[2]=0;
// arr=null;
// System.out.println(arr[0]);
// } catch (ArrayIndexOutOfBoundsException e) {
// e.printStackTrace();
// } catch(NullPointerException e1){
// e1.printStackTrace();
// }
MyException mye = new MyException();
mye.testLog();
}
}
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class ExceptionDemo1 {
public void run() {
NullPointerException n = new NullPointerException();
throw n;
}
public void run1() {
// try {
// System.out.println("AAAAAAAAAAAA");
run();
// } catch (Exception e) {
// System.out.println("捕获该异常");
// }
}
public void run2() {
System.out.println("我要在Run2方法中处理异常");
try {
run1();
} catch (Exception e) {
e.printStackTrace();
System.out.println("捕获该异常");
}
}
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
Logger logger = Logger.getLogger(ExceptionDemo1.class);
logger.debug(" debug ");
logger.error(" error ");
ExceptionDemo1 ed = new ExceptionDemo1();
ed.run2();
}
}
public class SampleExceptionInclude {
public static void main(String[] args) {
try {
test1(1);
try {
test2();
} catch (Exception e) {
System.out.println(e.getMessage());
}
// test2的异常處理完之后会运行以下的代碼
test1(-1);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
System.out.println("------華麗的-----------------------分割線-----");
try {
test1(-1);
} catch (ArithmeticException e) {
System.out.println("test1 err");
try {
test2();
} catch (Exception e1) {
System.out.println(e.getMessage());
}
}
System.out.println("------華麗的-----------------------分割線-----");
try {
test1(-1);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} finally {
try {
test2();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
private static void test1(int num) {
if (num <= 0) {
throw new ArithmeticException("num is 0 err1");
}
}
private static void test2() throws Exception {
throw new Exception("test2 err");
}
}