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{}
篇幅太多:------图在下篇
阅读全文
0 0
- Java---12种常用异常产生与分析--(附异常体系图)
- Java---12种常见异常体系图
- 异常体系与异常处理
- java异常5-异常体系
- java异常体系、自定义异常
- JAVA【异常一】异常体系
- Java异常体系unchecked异常check异常
- Java异常体系
- JAVA 异常体系
- java的异常体系
- Java异常体系
- java异常体系
- java异常体系
- Java的异常体系
- Java异常知识结构体系
- Java异常体系
- Java的异常体系
- Java中的异常体系
- Monthly Expense(二分)
- 如何在ABBYY FineReader 12中添加图像
- ubuntu sudo update与upgrade的作用及区别
- 网易2018校招编程题集合
- 阿里JAVA开发面试常问问题总结
- Java---12种常用异常产生与分析--(附异常体系图)
- Push对app运营的影响(二)
- openstack storlet文档(三):storlet术语和角色
- javascript高级程序设计第三版 第9章 客户端检测
- C# 输出一个 实心菱形&&空心菱形
- C语言:指针做函数参数
- MySql 时间处理
- Molar Mass, ACM/ICPC Seoul 2007,Uva1586 给出一种物质的分子式,求分子量
- 组合控件-自定义属性