文章目录
异常
概念
在计算机语言进行项目开发的过程中,即使程序员把代码写的尽善尽美,在系统的运行过中任然会遇到一些问题,因为很多问题不是靠代码能避免的
Eg: 客户输入数据的格式,读取文件是否存在,网络是否始终通畅等
Error:
java虚拟机无法解决的问题,
Eg: jvm系统内部错误,资源耗尽等情况
Error错误通过代码不可解决 这是硬件问题
Exception:
因编程错误或偶然产生的外在因素导致一般性问题, 可以针对代码来解决
Eg:空指针访问,读取不存在的文件,网络连接中断
解决方法:
一: 遇到异常就终止程序的运行
二: 在编写程序时,考虑到异常的检测,异常的提示,以及异常的处理.
捕获异常最理想的是在编译期间,但有的异常只有在运行时才会发生。比如:除数为0,数组下标越界等
体系
Throwable类有两个直接子类:Exception类、Error类。
Error表示错误,可能是编译期错误或者系统错误,往往程序中并不处理。
Exception表示异常,是所有异常类的父类,是程序员所关心的。
运行期异常:
程序运行时抛除的异常,所有RuntimeException的子类都是运行期异常
数学异常
空指针异常
数组下标越界
……
编译期异常(Checked Exception):
除去运行期的异常都是编译期异常,也称为检测异常
IOException
SQLException
……
异常体系图
异常处理
Java的异常处理是通过5个关键字来实现的:try、catch、 finally、throw、throws
捕获异常的有关信息:
与其它对象一样,可以访问一个异常对象的成员变量或调用它的方法。
getMessage()
获取异常信息,返回字符串
printStackTrace()
获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
基本语法(个人)
try{
可能会发生异常的代码
}catch(异常类型 引用名){
异常处理代码
}finally{
必须执行代码
}
try
检测不安全的代码块(发现异常)
try块中任何一条语句发生了异常,下面的代码将不会被执行,程序将跳转到异常处理代码块中,即catch块。因此,不要随意将不相关的代码放到try块中,因为随时可能会中断执行。
catch
把抓到的类型匹配的异常捕获,保证程序能继续运行下去
catch语句必须紧跟着try语句之后,称为捕获异常,也就是异常处理函数,一个try后面可以写多个catch,分别捕获不同类型的异常,要从子类往父类的顺序写,否则有编译错误
finally
finally该内容总是会执行的,只能有一个finally语句
finally{
必须执行的逻辑
}
第一种
try{
}catch(){
System.out.println("");
}
}
int a = 10;
int b = 2;
int c = 0;
try {
System.out.println(a/c);
try{
String s = "dasd";
s.length();
}catch (NullPointerException n1){
System.out.println("空指针异常");
}
}catch (ArithmeticException a1){
System.out.println("算数异常");
}
System.out.println("aaaa");
第二种
try{
try{
}catch(){
System.out.println("");
}
}catch(){
System.out.println("");
}
}
int a = 10;
int b = 2;
int c = 0;
try {
System.out.println(a/c);
try{
String s = "dasd";
s.length();
}catch (NullPointerException n1){
System.out.println("空指针异常");
}
}catch (ArithmeticException a1){
System.out.println("算数异常");
}
System.out.println("aaaa");
}
第三种
try{
}catch(){
System.out.println("");
}catch(){
System.out.println("");
}
int a = 10;
int b = 2;
int c= 0;
String s = "smlz";
s.length();
System.out.println("这里是try{}catch{}前的代码");
try {
System.out.println("a/b= " + a/b);
int i = Integer.parseInt("sadasd");
}catch (ArithmeticException a1){
System.out.println("算数异常");
}catch (NullPointerException n1){
System.out.println("空指针异常");
}catch (Exception e){
System.out.println("抱歉,系统繁忙");
//e.printStackTrace();
System.out.println("异常原因: " + e.getMessage());
}
System.out.println("这里是try{}catch{}外的代码");
}
第四种
try{
}catch(){
}finally{
}
按顺序往下走
finally的return把catch的return覆盖了
public static int test1(){
int a = 10;
int b = 0;
try {
System.out.println(a/b);
}catch (Exception e){
System.out.println("抱拳,系统繁忙");
return 0;
}finally {
System.out.println("请您在下午15:00时再来登记");
return 1;
}
第五种
try{
}catch(){
}finally{
}
当finally中没有return 那么后面可以继续写代码
当代码走到catch就不走了 停了
public static int test2(){
System.out.print("\n");
int a = 10;
int b = 0;
try {
System.out.println(a/b);
}catch (Exception e){
System.out.println("抱拳,系统繁忙");
return 0;
}finally {
System.out.println("请您在下午15:00时再来登记");
}
System.out.println("无异常代码");
return 1;
}
第六种
try{
}catch(){
}finally{
}
当finally中有return 那么后面就不用继续写代码
public static int test3(){
System.out.print("\n");
int a = 10;
int b = 0;
try{
System.out.println(a/b);
}finally {
System.out.println("请在下午15:00时再来登记");
return 1;
}
}
4-6调用
public static void main(String[] args) {
System.out.println(test1());
System.out.println(test2());
System.out.println(test3());
}
/*
抱歉,系统繁忙
请您在下午15:00时再来登记
1
抱歉,系统繁忙
请您在下午15:00时再来登记
0
请在下午15:00时再来登记
1
throws
定义方法的时候声明,不处理异常,交给调用方法处理
public void test throws 异常1,异常2,异常3{
}
任何方法都可以使用throws关键字声明异常类型,包括抽象方法。
子类重写父类中的方法,子类方法不能声明抛出比父类类型更大的异常。
使用了throws的方法,调用时必须处理声明的异常,要么使用try-catch,要么继续使用throws声明。
public static int test(int a,int b) throws ArithmeticException{
return a/b;
}
public static void main(String[] args) {
//在调用方法的时候处理
try{
test(4,0);
}catch (ArithmeticException a){
System.out.println("算术异常");
System.out.println(a.getMessage());
}
}
public abstract class ThrowsDemo2 {
abstract void test1() throws RuntimeException;
abstract void test2() throws ArithmeticException;
}
public class ThrowsDemo3 extends ThrowsDemo2{
@Override
void test1() throws RuntimeException {
}
@Override
void test2() throws RuntimeException {
}
}
throw
throw关键字用于显式抛出异常,抛出的时候是抛出的是一个异常类的实例化对象.
在异常处理中,try语句要捕获的是一个异常对象,那么此异常对象也可以自己抛出。
语法:throw throw new 异常类构造方法
如: throw new RunTimeException();
public static void someMethod() {
if (1==1) {
throw new RuntimeException(“错误原因”);
}
}
public static int test(int grade) throws RuntimeException{
if (grade>100 || grade<0){
throw new RuntimeException("成绩不合法");
}
return grade;
}
public static void main(String[] args) {
try {
test(101);
}catch (RuntimeException r){
System.out.println("成绩不合法");
}
}
自定义异常
基本语法
public class 异常类名 extends Exception/RuntimeException{
public 异常类名(String msg){
super(msg);
}
}
自定义异常类中往往不写其他方法,只重载需要使用的构造方法
继承Exception,在方法中使用throw抛出后,必须在方法中try-catch或throws抛出
public class ScoreException extends Exception{
public ScoreException() {
}
public ScoreException(String message) {
super(message);
}
}
public static int test(int grade) throws ScoreException{
if (grade>100 || grade<0){
throw new ScoreException("成绩不合法");
}
return grade;
}
public static void main(String[] args) {
try {
test(101);
}catch (ScoreException s){
s.printStackTrace();
System.out.println(s.getMessage());
}
}