zoukankan      html  css  js  c++  java
  • 异常

    异常

    异常就是Java程序在运行过程中出现的错误

    异常分类

    • Throwable
    • Error
      • 服务器宕机, 数据库崩溃等
    • Exception

    异常继承体系

    graph TD A[Throwable] A --> B[Error] A --> C[Exception] C --> D[RubtimeException]

    JVM默认处理机制

    • main函数收到问题,两种处理方式:
      • 自己将问题处理, 然后运行
      • 自己没有针对处理的方式,交给调用main的jvm处理, jvm有一个默认的异常处理机制,就将该异常处理并将该异常的名称,异常的信息,异常出现的位置打印到控制台,同事将程序停止运行

    异常处理

    方式

    • try ... catch ... finally

      • try catch
      • try catch finally
      • try finally

      try 检测异常

      catch 捕获异常

      finally 释放资源

    • throws

    编译期异常和运行期异常的区别

    • java中异常别分为两大类:编译时异常和运行时异常
    • 所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异常就是编译时异常

    编译时异常

    • Java程序必须显示处理,否则程序就会发生错误,无法通过编译

    运行时异常

    • 无需显示处理,也可以和编译时异常一样处理

    Throwable

    Throwable常用方式

    getMessage()

    • 获取异常信息返回字符串

    toString()

    • 获取异常类名和异常信息,返回字符串

    printStackTrace()

    • 获取异常类名和异常信息, 以及异常出现在程序中的位置,返回void
    public class Demo_Throwable {
    	public static void main(String[] args) {
    		try {
    			System.out.println(1/0);
    		} catch (Exception e) {
    			// 获取异常信息
    			System.out.println(e.getMessage());
    			
    			// 获取异常类名和异常信息,返回字符串
    			System.out.println(e.toString());
    			
    			// 获取异常类名和异常信息,以及异常出现在程序中的位置,返回void
    			e.printStackTrace();
    		}
    	}
    }
    

    Trows

    Trows的方式处理异常

    • 定义功能方式是,需要把出现的问题暴露出来让调用者去处理
    • 通过throws在方法上标识
    public class Demo6 {
    
    	public static void main(String[] args) throws Exception {
    		DemoPerson6 person = new DemoPerson6();
    		person.setAge(-17);
    		System.out.println(person.getAge());
    	}
    
    }
    class DemoPerson6{
    	private String name;
    	private int age;
    	
    	public DemoPerson6() {
    		super();
    	}
    
    	public DemoPerson6(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	/**
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    
    	/**
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	/**
    	 * @return the age
    	 */
    	public int getAge() {
    		return age;
    	}
    
    	/**
    	 * @param age the age to set
    	 * @throws Exception 
    	 */
    	public void setAge(int age) throws Exception {
    		
    		int i = 150;
    		if(age > 0 && age < i) {
    			this.age = age;
    			}
    		else {
    			throw new Exception("年龄非法");
    		}
    	}
    	
    	@Override
    	public String toString() {
    		return "Demo_Person6 [name=" + name + ", age=" + age + "]";
    	}
    }
    

    Trow

    在功能方法内部出现某种情况, 程序不能继续运行, 需要进行跳转,就用trow把异常对象抛出

    Trows和Trow区别

    Trows

    • 用在方法声明后面, 跟的是异常类名
    • 可以更多个异常类名,用逗号隔开
    • 表示抛出异常,又该方法的调用者来处理

    Trow

    • 用在方法体内,跟的是一场对象名
    • 只能抛出一个异常对象名
    • 表示抛出异常,有方法体内的语句处理
    package com.mephisto.exception;
    /**
     * 
     * @author mephisto
     *
     */
    public class Demo6 {
    
    	public static void main(String[] args) throws Exception {
    		DemoPerson6 person = new DemoPerson6();
    		person.setAge(-17);
    		System.out.println(person.getAge());
    	}
    
    }
    class DemoPerson6{
    	private String name;
    	private int age;
    	
    	public DemoPerson6() {
    		super();
    	}
    
    	public DemoPerson6(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	/**
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    
    	/**
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	/**
    	 * @return the age
    	 */
    	public int getAge() {
    		return age;
    	}
    
    	/**
    	 * @param age the age to set
    	 * @throws Exception 
    	 */
    	public void setAge(int age) throws Exception {
    		
    		int i = 150;
    		if(age > 0 && age < i) {
    			this.age = age;
    			}
    		else {
    			throw new Exception("年龄非法");
    		}
    	}
    	
    	@Override
    	public String toString() {
    		return "Demo_Person6 [name=" + name + ", age=" + age + "]";
    	}
    }
    

    finally

    特点

    • 被finally控制的语句体一定会执行
    • 特殊情况: 在执行到finally之前jvm退出了(比如System.exit(0))

    作用

    • 用于释放资源,在IO流操作和数据库操作中会见到
    public class Demo_Finally {
    	public static void main(String[] args) {
    		try {
    			System.out.println(10/0);
    		} catch (Exception e) {
    			System.out.println("除数为零");
    			return ;   //  先执行finally语句中的, 然后再return
    			// System.exit(0);	// 不执行finally中的语句, 因为System.exit(0)是退出jvm虚拟机
    		}finally {
    			System.out.println("执行了吗");
    		}		
    	}
    }
    

    final, finally和finalize区别

    final

    • final可以修饰类,不能被继承
    • final修饰方法, 该方法不能被重写
    • final修饰变量,只能被赋值一次

    finally

    • finally是try语句中的一个语句体,不能单独使用, 用来释放资源

    finalize

    • 当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。

    catch和finally问题

    catch里面有return 语句, finally代码会在return之前运行

    public class Demo1 {
    
    	public static void main(String[] args) {
    		DemoTest1 demoTest1 = new DemoTest1();
    		
    		System.out.println(demoTest1.mothd());
    	}
    
    }
    
    class DemoTest1{
    	public int mothd() {
    		int x = 10;
    		try {
    			x = 20;
    			System.out.println(x/0);
    			return x;
    		} catch (Exception e) {
    			x = 30;
    			return x;
    		}finally {
    			x = 40;   // 没有意义, 而且最好不要在finally写return
    			System.out.println("finally");
    		}	
    	}
    }
    

    自定义异常

    更快速的定位异常

    public class Demo_MyException {
    
    	public static void main(String[] args) throws AgeOutOfBoundException {
    		DemoPerson person = new DemoPerson();
    		person.setAge(-18);
    		System.out.println(person.getAge());
    
    	}
    
    }
    
    class DemoPerson {
    	private String name;
    	private int age;
    
    	public DemoPerson() {
    		super();
    	}
    
    	public DemoPerson(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	/**
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    
    	/**
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	/**
    	 * @return the age
    	 */
    	public int getAge() {
    		return age;
    	}
    
    	/**
    	 * @param age the age to set
    	 * @throws AgeOutOfBoundException 
    	 */
    	public void setAge(int age) throws AgeOutOfBoundException {
    		if (age > 0 && age < 150) {
    			this.age = age;
    		}else {
    			throw  new AgeOutOfBoundException("年龄非法");
    		}
    
    	}
    
    	@Override
    	public String toString() {
    		return "DemoPerson [name=" + name + ", age=" + age + "]";
    	}
    
    }
    
    class AgeOutOfBoundException extends Exception {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public AgeOutOfBoundException() {
    		super();
    	}
    
    	public AgeOutOfBoundException(String message) {
    		super(message);
    	}
    }
    

    注意事项

    • 子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类
    • 如果父类抛出了多个异常子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常
    • 如果被重写的方法没有抛出异常,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能用try而不能用throws

    使用异常处理

    原则

    • 如果该功能内部可以将问题处理掉,用try,如果处理不了,交给调用者处理,这里用throws

    区别

    • 后续程序需要继续运行就用try
    • 后续程序不需要运行就用throws

    如果JDK没有提供对应的异常,需要自定义异常

  • 相关阅读:
    Git使用感悟
    Just open sourced the JavaScript Inlay Hints Provider function in IDEA plugin React Native Console commercial version
    Flutter和React Native组件层级对比
    React Native 迁移到 Flutter 心得体会[持续更新中]
    IntelliJ IDEA 2020.3 EAP 新特性: 拖动分割编辑器,新欢迎窗口等
    Universal Abstract Syntax Tree (UAST)
    Creating your first Flutter project using WebStorm 2020
    WIN10系统跑流量,彻底杜绝win10更新程序后台下载
    linux centos7安装mysql5.7(安装版)教程
    VMware启动虚拟机一直处于黑屏且无反应
  • 原文地址:https://www.cnblogs.com/mephisto03/p/9473961.html
Copyright © 2011-2022 走看看