目录
数组索引越界异常 ArrayIndexOutOfBoundsException
字符串索引越界异常 StrigIndexnOutOfBoundsException
1、public void printStackTrace() :
2、public String getMessage() :
try{}catch(){}catch(){}catch{}
try{}catch(){}catch(){}finally{}
运行时期异常(即runtime异常、unchecked异常、非受检异常):
Java异常概述
什么是异常
程序运行中不正常情况
数组越界 int[] a = new int[5];
a[5];
程序在运行过程中,由于用户输入的数据格式有问题,读取文件,文件被强制删除,网络传输过程中断网了
就会导致程序出现不正常情况--->异常
注意:语法错误,并非异常
int a =10 后面忘加分号,属于程序写错了
广义异常:所有的问题 语法错误 error,exception
狭义异常:exception
Java中默认的异常处理机制
当程序出现异常后,会将异常的信息包装在一个对应的对象中,并抛出此对象,并终止程序的运行
异常示例
数组索引越界异常 ArrayIndexOutOfBoundsException
package com.ffyc.javaexception;
public class Demo1 {
public static void main(String[] args) {
int[] a = new int[5];
a[5] = 5;
System.out.println("后面的程序");
}
}
字符串索引越界异常 StrigIndexnOutOfBoundsException
package com.ffyc.javaexception;
public class Demo1 {
public static void main(String[] args) {
String s = "abcd";
s.charAt(4);
System.out.println("后面的程序");
}
}
算术异常 ArithmeticException
package com.ffyc.javaexception;
public class Demo1 {
public static void main(String[] args) {
int a = 10;
int b = 0;
int c =a/b;
System.out.println("后面的程序");
}
}
空指针异常 NullPointerException
package com.ffyc.javaexception;
public class Demo1 {
public static void main(String[] args) {
String s = null;
s.length();
System.out.println("后面的程序");
}
}
类型转换异常 ClassCastException
package com.ffyc.javaexception;
public class Demo1 {
public static void main(String[] args) {
Object s = "abcd";
Integer a = (Integer)s;
System.out.println("后面的程序");
}
}
数字格式化异常 NumberFormatException
package com.ffyc.javaexception;
public class Demo1 {
public static void main(String[] args) {
int a = Integer.parseInt("abc");
System.out.println("后面的程序");
}
}
异常信息图解
异常的处理
1、遇到异常终止程序的运行(不想要的)
2、遇到程序异常时,进行处理
Java异常体系结构
java.lang.Throwable 是java异常体系中的根类
图示
两个子类:
java.lang.Error:
错误 是java程序无法处理的,例如内存不够用了
示例
堆溢出
package com.ffyc.javaexception;
import java.util.ArrayList;
import java.util.Date;
public class Demo2 {
public static void main(String[] args) {
//Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
内存不够用,程序无法处理
ArrayList arrayList = new ArrayList();
while (true){
arrayList.add(new Date());
}
}
}
栈溢出
package com.ffyc.javaexception;
public class Demo2 {
public static void main(String[] args) {
//java.lang.StackOverflowError 栈溢出
sum(100000);
}
public static int sum(int n){
if(n == 1){
return 1;
}
return sum(n-1);
}
}
java.lang.Exception:
异常 出现的一般性问题,可以使用java中的异常处理机制进行处理的
两个常用的方法:
1、public void printStackTrace() :
打印栈中异常的信息
2、public String getMessage() :
获取到异常的信息
常见的异常
异常--运行时异常
数组越界异常
类型转换异常
数字格式化异常
空指针异常
算术异常
异常处理
java中提供的一套异常处理机制,在程序发生异常时,可以执行预先设定好的处理程序,执行完成后,程序会停止,可以继续向后执行
在写代码的时候,就要根据不同的情况设定好处理程序,
运行程序
如果程序执行时,出现问题,执行异常处理程序
如果程序执行时,没有出现问题,不需要执行异常处理程序
关键字
try{}catch(){}fianlly{}
try{
写代码,可能会出现异常
}catch(异常类型 a){ 捕获指定类型的异常
}finally{
最终必须要执行的代码
}
try{}catch(){}
package com.ffyc.javaexception;
public class Demo3 {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int c = a/b;
}catch (ArithmeticException a){//捕获出现的指定的异常对象,如果类型不匹配,就捕获不到
a.printStackTrace();//在开发期间,建议使用printStackTrace()打印异常信息
System.out.println("除数不能为0!");
}
System.out.println("后面的代码");
}
}
try{}catch(){}catch{}
package com.ffyc.javaexception;
public class Demo3 {
public static void main(String[] args) {
try {
String s = null;
s.length();
int a = 10;
int b = 0;
int c = a/b;
}catch (ArithmeticException a){
a.printStackTrace();
System.out.println("除数不能为0!");
}catch (NullPointerException n){
n.printStackTrace();
System.out.println("对象为null");
}
System.out.println("后面的代码");
}
}
异常代码执行流程
try{}catch(){}catch(){}catch{}
package com.ffyc.javaexception;
public class Demo3 {
public static void main(String[] args) {
try {
//写可能出现异常的代码,需要长期积累
Integer.parseInt("abc");
String s = null;
s.length();
int a = 10;
int b = 0;
int c = a/b;
}catch (ArithmeticException a){
a.printStackTrace();
System.out.println("除数不能为0!");
}catch (NullPointerException n){
n.printStackTrace();
System.out.println("对象为null");
}catch (Exception e){//父类类型必须写在多个catch的最下面
e.printStackTrace();
System.out.println("程序异常:"+e.getMessage());
}
System.out.println("后面的代码");
}
}
try{}catch(){}finally{}
package com.ffyc.javaexception;
public class Demo4 {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int c = a/b;
}catch (ArithmeticException a){
Integer.parseInt("abc");
a.printStackTrace();
System.out.println("算术异常");
}finally {
System.out.println("必须执行的代码");
}
System.out.println("后面的代码");
}
}
try{}catch(){}finally{}
package com.ffyc.javaexception;
public class Demo5 {
public static void main(String[] args) {
int result = chu(10,0);
System.out.println(result);
System.out.println("后面的代码");
}
public static int chu(int a,int b){
try {
int c = a/b;
return c;
}catch (ArithmeticException ar){
ar.printStackTrace();
return -1;
}finally {
System.out.println("关闭流通道");
}
}
}
try{}catch(){}catch(){}finally{}
package com.ffyc.javaexception;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo6 {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = null;
try {
//连接文件
inputStream = new FileInputStream("D:/a.txt");
inputStream.read();//读
}catch (FileNotFoundException f){
f.printStackTrace();
System.out.println("文件找不到");
}catch (IOException i){
i.printStackTrace();
System.out.println("读文件异常");
}finally {//在finally代码块中必须关闭流通道
if(inputStream!=null){
inputStream.close();
}
}
}
}
throws
在方法声明的地方,通过throws关键字 声明此方法可能会抛出异常
使用throws抛出异常对象,表示此方法不处理异常,交给调用这个方法的地方进行处理
一般在底层方法中,都不进行处理,抛出异常即可
运行期间异常
由于chu的方法,抛出的是一个运行时异常,所以在编译期间不会强制处理,需要程序员注意
package com.ffyc.javaexception;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Demo7 {
public static void main(String[] args) {
try {
chu(10,0);
}catch (ArithmeticException a){
a.printStackTrace();
System.out.println("除数不能为零");
}
System.out.println("耶");
}
public static int chu(int a,int b)throws ArithmeticException{
int c = a/b;
return c;
}
}
编译期间异常
由于test方法抛出的是一个编译期异常,所以在编写代码期间,就强制要求进行处理。
处理方法有两种1、try catch 捕获处理 2、继续throws 一般到顶层的地方就不能再进行throws
package com.ffyc.javaexception;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Demo7 {
public static void main(String[] args) {
try {
test();
} catch (UnsupportedEncodingException | ParseException e) {
e.printStackTrace();
}
System.out.println("耶");
}
public static void test() throws UnsupportedEncodingException, ParseException {
"abc".getBytes("utf-8");
new SimpleDateFormat("aa").parse("258216—424");
}
}
throw
throws:
用在方法声明部分,表示此方法有可能出现某种类型的异常,此方法不处理异常,谁调用谁处理
throw:
在方法体中,抛出一个具体的异常对象,该方法终止运行,在异常对象中的构造方法中自定义异常原因
package com.ffyc.javaexception;
public class Demo8 {
public static void main(String[] args) { );
try {
char c = level(101);
}catch (ArithmeticException a){
a.printStackTrace();
System.out.println(a.getMessage());
}
System.out.println("111");
}
public static char level(int score) throws ArithmeticException{
if(score<0 || score>100){
//当不满足某种条件时,程序中主动的抛出异常对象,终止此方法向下执行,通过异常构造方法传入异常原因
throw new ArithmeticException("非法的分数");
}
if(score>90 || score<100){
return 'A';
}else {
return 'B';
}
}
}
运行期间异常和编译期异常
编译时期异常(即checked异常、受检异常):
在代码编译阶段,编译器就能明 确警示当前代码可能发生(不是一定发生)XX异常,并明确督促程序员提前编写 处理它的代码。如果程序员没有编写对应的异常处理代码,则编译器就会直接判 定编译失败,从而不能生成字节码文件。通常,这类异常的发生不是由程序员的 代码引起的,例如:FileNotFoundException(文件找不到异常)。
运行时期异常(即runtime异常、unchecked异常、非受检异常):
在代码编译 阶段,编译器完全不做任何检查,无论该异常是否会发生,编译器都不给出任何 提示。只有等代码运行起来并确实发生了XX异常,它才能被发现。通常,这类异 常是由程序员的代码编写不当引起的,只要稍加判断,或者细心检查就可以避免。
java.lang.RuntimeException类及它的子类都是运行时异常。比如: ArrayIndexOutOfBoundsException数组下标越界异常, ClassCastException类型转换异常。
自定义异常
package com.ffyc.javaexception;
public class Demo9 {
public static void main(String[] args) {
try {
char c = level(101);
}catch (ScoreException s){
s.printStackTrace();
System.out.println(s.getMessage());
}
System.out.println("111");
}
public static char level(int score) throws ScoreException{
if(score<0 || score>100){
throw new ScoreException("非法的分数");
}
if(score>90 || score<100){
return 'A';
}else {
return 'B';
}
}
}
根据自己的业务需要定义异常类型---自定义异常
分数自定义异常
当分数不满足条件时,抛出此类的对象
例如百分制分数,当输入小于0大于100时抛出
package com.ffyc.javaexception;
public class ScoreException extends Exception{
public ScoreException(){
}
public ScoreException(String message) {
super(message);
}
}