Java---12种常用异常产生与分析--(附异常体系图)

来源:互联网 发布:中国有哪些公知 编辑:程序博客网 时间:2024/06/02 18:12

Java---12种常用异常产生与分析

常见运行异常(7种):

ArithmeticException,ArrayStoreEcxeption,ClassCastException,IllegalArgumentException

IndexOutOfBoundsException,NoSuchElementException,NullPointerException
常见编译异常(5种):

ClassNotFoundException,FileNotFoundException,IOException,ParseException,UnsupportedEncodingException

import java.awt.Color;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.Iterator;import java.util.List;public class Test5 {//Java12种常用异常产生与分析--(附异常体系图)public static void main(String[] args)  {//运行异常(在程序运行时才会产生的异常)//①.ArithmeticException ---数学运算异常//Exception in thread "main" java.lang.ArithmeticException: / by zeroint a=6;int b=a/0;//②.ArrayStoreEcxeption  ---数组存储异常//Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer Object x[] = new String[3]; Integer aa=new Integer(5); //此处有自动装箱过程 x[0] = 11; //③.ClassCastException ---类转化异常//Exception in thread "main" java.lang.ClassCastException: a20180811.Fa cannot be cast to a20180811.Son Son son=(Son)new Fa();//④.new IllegalArgumentException(); ---参数异常// java.lang.IllegalArgumentException: Color parameter outside of expected range: Green Color c = new Color(0,258,0);//⑤.IndexOutOfBoundsException ---下表越界异常//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3String[] str=new String[3];str[0]="xiaoming";str[1]="ff";str[2]="ee";str[3]="ff";//⑥.NoSuchElementException  ---元素不存在异常//Exception in thread "main" java.util.NoSuchElementExceptionList<Integer> list=new ArrayList<Integer>();list.add(111);list.add(222);Iterator<Integer> it = list.iterator();while(it.hasNext()){it.next();it.next();it.next();}//⑦.NullPointerException ---空指针异常//Exception in thread "main" java.lang.NullPointerExceptionString strr=null;System.out.println(strr.toCharArray());//编译时异常 // ①.ClassNotFoundException ---类未找到异常try {Class.forName("com.args");} catch (ClassNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}//②.FileNotFoundException   ---文件未找到异常File file=new File("d:/args");FileInputStream fis=null;FileOutputStream fos=null;try {fis = new FileInputStream(file);fos = new FileOutputStream("d:/dd");} catch (FileNotFoundException e2) {// TODO Auto-generated catch blocke2.printStackTrace();}int n;//③.IOException    ---文件流异常try {while((n=fis.read())!=-1){fos.write(n);}} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}finally{try {fos.close();fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//④.ParseException   ---格式转化异常SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");try {Date parse = sdf.parse("2018-09-03");String format = sdf.format(parse);System.out.println(format);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}//⑤.UnsupportedEncodingException   ---未提供的编码集异常byte[] by=new byte[]{1,3,5,6};try {String string = new String(by,"jbk");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}class Fa{}class Son extends Fa{}

篇幅太多:------图在下篇
原创粉丝点击