本文共 4966 字,大约阅读时间需要 16 分钟。
一、Java异常(一) Java异常简介及其架构
1、自定义异常
public class ExceptionTest {public static void main(String[] args) { // TODO Auto-generated method stub // Integer it=5; Integer it = null; try { System.out.println("aaaaaaa"); if (true) {// 自己定义的异常 throw new RuntimeException("出差类"); } // int i=it; // System.out.println("版斑斑驳驳"); // int [] nums= {2,3,4,5}; // System.out.println(nums[6]); } catch (RuntimeException e) { System.out.println("wrong" + e.getMessage()); e.printStackTrace();// 把错误信息打印在控制台,这不是处理异常的好方法,不能随便用 // 处理异常,但是里面什么也没有写,这是吃异常,不能这么写 } /* * catch(NullPointerException e ) { System.out.println("i不能为null"); * }catch(ArrayIndexOutOfBoundsException e) { System.out.println("超出索引号"); } * catch(NullPointerException | ArrayIndexOutOfBoundsException e) { * System.out.println("发生❌");//jdk1.7以后支持这种写法 } */ System.out.println("cccccc");}
}
2、在main方法里面调用异常
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class ExceptionTest2 {
public static void main(String[] args) throws FileNotFoundException, ParseException { // TODO Auto-generated method stub try { if(true) { //throw new RuntimeException();//抛出来RuntimeException,可以不用处理, //throw new FileNotFoundException();//除了RuntimeException和其子类外,exception其他异常都要处理 throw new IOException(); } }catch(FileNotFoundException f) { f.printStackTrace(); }catch(IOException i) { i.printStackTrace(); }/* SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//自己处理异常 Date date; try { date = sdf.parse("2015-03-14"); System.out.println(date); FileInputStream fis=new FileInputStream("w:\\1.txt"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("解析日期出错"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("文件未读取"); }*//* SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2015-03-14"); System.out.println(date); FileInputStream fis=new FileInputStream("w:\\1.txt");*/ //在main处抛出异常 try { //自己处理异常 test(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } test();//在main处抛出异常}static void test() throws ParseException, FileNotFoundException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//把异常抛出去,有调用者处理 Date date = sdf.parse("2015-03-14"); System.out.println(date); FileInputStream fis=new FileInputStream("w:\\1.txt");}
}
3、调用继承的异常
public class ExceptionTest3 {public static void main(String[] args) { // 自定义异常 try { test(1);//调用者处理异常类 } catch (MyException2 e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("chuwuo"+e.getMessage()); }}static void test(int i) throws MyException2 { if(i<10) { //throw new IllegalArgumentException("wrong");//runtimeexception 不用处理 //throw new MyException1("wrong wrong "); MyException1是runtimeexception子类,不用处理 throw new MyException2("wrong wrong ");//MyException2是exception的子类,异常要处理 //抛出去让调用着处理 }}
}
public class MyException1 extends RuntimeException{
public MyException1() { super(); // TODO Auto-generated constructor stub}public MyException1(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub}public MyException1(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub}public MyException1(String message) { super(message); // TODO Auto-generated constructor stub}public MyException1(Throwable cause) { super(cause); // TODO Auto-generated constructor stub}
}
public class MyException2 extends Exception {
public MyException2() { super(); // TODO Auto-generated constructor stub}public MyException2(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub}public MyException2(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub}public MyException2(String message) { super(message); // TODO Auto-generated constructor stub}public MyException2(Throwable cause) { super(cause); // TODO Auto-generated constructor stub}
}
转载于:https://blog.51cto.com/14394144/2410181