/*Error
* Exception:运行时异常(),编译时异常();
* 异常Exception
* 对一段代码进行监控
* try{
* 要监控的代码
* }catch(异常){
* 出现了异常后的处理
* }
*
* */
package com.ly.cn01;
import org.junit.Test;
public class Dome {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
/*Error
* Exception:运行时异常(),编译时异常();
* 异常Exception
* 对一段代码进行监控
* try{
* 要监控的代码
* }catch(异常){
* 出现了异常后的处理
* }
*
* */
// try {
// method3();
// } catch (Exception e) {
//
// System.out.println("异常了。。。");
// }
method4();
// method5(null);
}
@Test
public void method1(){
try{
System.out.println("回家过年");
System.out.println("吃好吃的");
Object s=null;
s.toString();
// int i=5/0;
System.out.println("玩好玩的");
System.out.println("********");
}catch(ArithmeticException e){
e.printStackTrace();
System.out.println("封层");
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("击毙");
}finally{
System.out.println("饭还是要吃的");
}
}
@Test
public void method2(){
try{
Class.forName("");
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("乱棍打死");
}
}
public static void method3(){
try {
System.out.println(5/0);
} catch (Exception e) {
System.out.println("自己犯错自己承担");
}
}
// throws 把异常往上跑,谁调用谁处理。
public static void method4() throws Exception{
Class.forName("");
}
// throw 方法体中主动抛出一个异常
public static void method5(Object o){
if(o==null){
throw new NullPointerException("参数不能为null");
}
System.out.println("正常功能。。");
}
}