java基础学习日志14-程序员宅基地

异常,和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");
	}

}


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Interest_Soul/article/details/79208550

智能推荐

Redis 命令-程序员宅基地

文章浏览阅读166次。Redis 命令Redis 命令用于在 redis 服务上执行操作。要在 redis 服务上执行命令需要一个 redis 客户端。Redis 客户端在我们之前下载的的 redis 的安装包中。语法Redis 客户端的基本语法为:$ redis-cli实例以下实例讲解了如何启动 redis 客户端:启动 redis 客户端,打开终端并输入命令 redis-cli。该命

个人笔记5-程序员宅基地

文章浏览阅读372次。个人笔记

POJ 2965 The Pilots Brothers' refrigerator (枚举)-程序员宅基地

文章浏览阅读955次。POJ 2965 The Pilots Brothers' refrigerator (枚举)

信驰达BLE 5.0低功耗蓝牙模块使用 (AT指令串口透传) RSBRS02ABR_蓝牙5.0能被串口助手检测到吗-程序员宅基地

文章浏览阅读1.5k次。信驰达蓝牙模组信驰达透传固件功能特点:使用简单,无需任何蓝牙协议栈应用经验;支持蓝牙 5.0 协议栈。价格便宜,价格便宜,价格便宜 (重要的事情只说三遍)1. 环境准备1.1 硬件环境USB转TTL 1个 + 杜邦线 6 根RSBRS02ABR 模块 1个 1.2软件准备准备驱动安装包CP210x_Universal_Windows_Driver:准备串口调试助手工具UartAssist : 下载链接准备手机APP:nRf-Connect _蓝牙5.0能被串口助手检测到吗

程序员练级攻略(转自coolshell 陈皓)-程序员宅基地

文章浏览阅读1.1k次,点赞2次,收藏8次。前言你是否觉得自己从学校毕业的时候只做过小玩具一样的程序?走入职场后哪怕没有什么经验也可以把以下这些课外练习走一遍(朋友的抱怨:学校课程总是从理论出发,作业项目都看不出有什么实际作用,不如从工作中的需求出发)建议:不要乱买书,不要乱追新技术新名词,基础的东西经过很长时间积累而且还会在未来至少10年通用。回顾一下历史,看看历史上时间线上技术的发展,你才能明白明天会是什么样。一

什么是机器学习,一文让你了解机器学习的历史和真谛-程序员宅基地

文章浏览阅读2.1w次,点赞19次,收藏58次。作者:计算机的潜意识在本篇文章中,我将对机器学习做个概要的介绍。本文的目的是能让即便完全不了解机器学习的人也能了解机器学习,并且上手相关的实践。这篇文档也算是 EasyPR开发的番外篇,从这里开始,必须对机器学习了解才能进一步介绍EasyPR的内核。当然,本文也面对一般读者,不会对阅读有相关的前提要求。在进入正题前,我想读者心中可能会有一个疑惑:机器学习有什么重要性,以至于要阅读完

随便推点

IDEA异常 cannot resolve symbol 'HttpServletRequest'-程序员宅基地

文章浏览阅读1.2w次,点赞15次,收藏14次。IDEA异常 cannot resolve symbol ‘HttpServletRequest’如图一在IDEA出现的问题,java编辑器找不到HttpServletRequest包,只需将tomcat中的库倒进来即可如图二。步骤file->project structure->modules->选择对应的项目->dependencies->右侧加号libraries->添加tomcat的libr

拦截器interceptors基本介绍-程序员宅基地

文章浏览阅读3k次。拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行.同时也是提供了一种可以提取action中可重用的部分的方式.拦截器必须是无状态的,不能保证为每一个请求或者action创建一个实例.拦截器可以选择短路一个action调用,然后返回一个结果码(如com.opensymphony.xwork_interceptors

视觉SLAM中动态场景剔除总结_视觉slam怎么去除动态特征点-程序员宅基地

文章浏览阅读5k次,点赞2次,收藏26次。目前看了两篇论文总结一下:DS-SLAM A Semantic Visual SLAM towards dynamic environments (2018):这篇文章基于ORB_SLAM2, 利用帧间图像的光流跟踪,进行一致性检验(利用了基础矩阵,判断点极线距离的大小),找到运动的像素点。同时另开辟一个线程做Segmet 场景语义分割(只能分割20种物体),认为人是可能运动的物体,如果运动的..._视觉slam怎么去除动态特征点

单源最短路径SSSP之Bellman-Ford算法-程序员宅基地

文章浏览阅读722次。Bellman-Ford算法和Dijkstra算法都是求解图的最短路径的算法。Bellman是求单源点到各个顶点的最短路径,适用条件为有向或无向图,权重可为负值。当存在负权环路时,算法返回一个false值。该算法效率比较低,需要对边进行 |V|- 1 次松弛操作Bellman-Ford算法寻找单源最短路径的时间复杂度为O(V*E)。(V为给定图的顶点集合,E为给定图的边集合)两者区别在于:..._sssp

Upgrade Oracle Database 11.1.0 to 12.1.0 for EBS R12.1.3_ins-32016-程序员宅基地

文章浏览阅读1.7k次。vm oracle linux7 安装 Oracle E-Business 12.1说明:在执行下面步骤之前,需要提前申请oracle账号,下载12.1介质。注册oracle账号链接:http://metalink.oracle.com/oracle linux7 和Oracle E-Business 12.1下载链接:https://edelivery.oracle.com文章目录vm oracle linux7 安装 Oracle E-Business 12.1一、操作系统要求1.1、在 v_ins-32016

第二章思维导图_今天的作业,自己绘制第二章有理数的思维导图,发到老师的作业小程序-程序员宅基地

文章浏览阅读572次。_今天的作业,自己绘制第二章有理数的思维导图,发到老师的作业小程序

推荐文章

热门文章

相关标签