zoukankan      html  css  js  c++  java
  • java 异常

    异常体系:
    ---| Throwable 所有错误或者异常的父类
    ------| Error(错误)
    ------| Exception(异常) 异常一般都通过代码处理
    --------| 运行时异常: 如果一个方法内部抛出了一个运行时异常,那么方法可以忽略这个异常,即可以 不处理,调用者也可以不处理,编译都能通过
    --------| 编译时异常: 如果一个方法内部抛出了一个编译时异常,那么方法上就必须要 捕获或抛出处理,如果是抛出处理,调用者也必须要处理,否则编译无法通过



    package com.czbk33.base.day11;

    /**
    * Created by chengtao on 17/10/18.
    *

    异常体系:
    --------| Throwable 所有错误或者异常的父类
    --------------| Error(错误)
    --------------| Exception(异常) 异常一般都通过代码处理
    ------------------| 运行时异常: 如果一个方法内部抛出了一个运行时异常,那么方法可以忽略这个异常,即可以 不处理,调用者也可以不处理。
    ------------------| 编译时异常: 如果一个方法内部抛出了一个编译时异常,那么方法上就必须要 捕获或抛出处理,如果是抛出处理,调用者也必须要处理。

    运行时异常: RuntimeException以及RuntimeException子类 都是属于运行时异常。
    编译时异常: 除了运行时异常就是编译异常。
    *
    *
    *
    *
    * 异常体系:Throwable 所有错误或者异常的父类Error(错误)Exception(异常) 异常一般都通过代码处理.
    运行时异常: 如果一个方法内部抛出了一个运行时异常,那么方法上 可以声明也可以不 声明,调用者可以以处理也可以不处理。
    编译时异常(非运行时异常、受检异常): 如果一个方法内部抛出了一个编译时异常对象,
    要么 在方法内 捕获处理
    要么 在方法上声明抛出,做抛出处理,而且调用者也必须要处理。
    运行时异常: RuntimeException以及RuntimeException子类 都是属于运行时异常。

    编译时异常: 除了运行时异常就是编译异常。
    区别:在编译时,是否检查 对该异常进行了处(捕获/抛出 处理均可)

    */
    public class TestTwoDifferntException {
    public static void main(String[] args) {
    TestTwoDifferntException entity = new TestTwoDifferntException();
    System.out.println("--------- 编译时异常 -----------");
    try {
    entity.methodComplie02();
    }catch (MyCompileException e){
    System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
    }

    System.out.println("--------- 运行时异常 -----------");
    try {
    entity.methodRun01();
    }catch (MyRunTimeException e){
    System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
    }

    try {
    entity.methodRun02();
    }catch (MyRunTimeException e){
    System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
    }

    try {
    entity.methodRun03();
    }catch (MyRunTimeException e){
    System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
    }

    try {
    entity.methodRun04();
    }catch (MyRunTimeException e){
    System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
    }
    }

    public void methodComplie00(int a ){
    if(a==0){
    // throw new MyCompileException("编译时异常",1);
    }
    }

    //1.编译时异常,必须处理(捕获/抛出 均可)
    public void methodComplie01(){
    // throw new MyCompileException("编译时异常",1);
    }

    //2.抛出 处理 编译时异常
    public void methodComplie02() throws MyCompileException{
    throw new MyCompileException("编译时异常",2);
    }

    //3.捕获 处理 编译时异常
    public void methodComplie03(){
    try {
    throw new MyCompileException("编译时异常", 3);
    }catch (Exception e){
    System.out.println("捕获 处理 编译时异常");
    }
    }


    //4.捕获 处理 编译时异常
    public void methodComplie04(){
    try {
    throw new MyCompileException("编译时异常", 4);
    }catch (MyCompileException e){
    System.out.println("捕获到的异常信息:--> " +"code = " + e.getCode() + ", message = " + e.getMessage());
    // throw e;
    }
    }


    //1.运行时异常,可以不做 处理(捕获/抛出 都不做)
    public void methodRun01(){
    throw new MyRunTimeException("运行时异常",1);
    }

    //2.抛出 处理 编译时异常
    public void methodRun02()throws MyRunTimeException{
    throw new MyRunTimeException("运行时异常",2);
    }

    //3.捕获 处理 编译时异常
    public void methodRun03(){
    try {
    throw new MyRunTimeException("运行时异常",3);
    }catch (MyRunTimeException e ){
    System.out.println("捕获到的异常信息:--> " +"code = " + e.getCode() + ", message = " + e.getMessage());
    }
    }

    //4.捕获 处理 编译时异常
    public void methodRun04(){
    try {
    throw new MyRunTimeException("运行时异常",4);
    }catch (MyRunTimeException e){
    System.out.println("捕获到的异常信息:--> " +"code = " + e.getCode() + ", message = " + e.getMessage());
    throw e;
    }
    }
    }
     

    --------------------------------
    
    
    --------------------------------
    
    
    



    package com.czbk33.base.day11;

    /**
    * Created by chengtao on 17/10/17.
    * InterruptedException 是一个编译时的异常
    */
    public class MyCompileException extends InterruptedException {
    private String message;
    private int code;

    public MyCompileException() {
    }

    public MyCompileException(String message, int code) {
    this.message = message;
    this.code = code;
    }

    @Override
    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    public int getCode() {
    return code;
    }

    public void setCode(int code) {
    this.code = code;
    }
    }



    --------------------------------
    --------------------------------


    package com.czbk33.base.day11;

    /**
    * Created by chengtao on 17/10/17.
    */
    public class MyRunTimeException extends RuntimeException {
    private String message;
    private int code;

    public MyRunTimeException() {
    }

    public MyRunTimeException(String message, int code) {
    this.message = message;
    this.code = code;
    }

    @Override
    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    public int getCode() {
    return code;
    }

    public void setCode(int code) {
    this.code = code;
    }
    }
     
  • 相关阅读:
    Mybatis Plus整合PageHelper分页的实现示例
    关于xlrd最新版本不支持.xlsx文件的解决办法
    mysql 错误解决大法 Specified key was too long; max key length is 767 bytes
    php连接redis
    flask通过request.path获取定义view函数的文件和行号
    使用bash内置命令complete来实现参数补全
    linux下对比两个文件夹下python文件的差异
    ssh登录后自动切换到原来的目录
    ssh &2>1 和重定向顺序问题
    wsl1(win10)中安装bochs
  • 原文地址:https://www.cnblogs.com/ctaixw/p/8076432.html
Copyright © 2011-2022 走看看