Java Exception

Exception

异常捕获

Java Exception_第1张图片

  • 将代码块选中->ctrl+alt+t->选中try-catch

01:

public class Exception01 {
     
    public static void main(String[] args) {
     
        int n1 = 10;
        int n2 = 0;


        try {
     
            int res = n1/n2;
        } catch (Exception e) {
     
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        System.out.println("continue");
    }
}

异常介绍

基本概念

Java Exception_第2张图片

异常体系图和小结

Java Exception_第3张图片

Java Exception_第4张图片

常见的运行时异常

Java Exception_第5张图片

01:

public class ExceptionDemo {
     
    public static void main(String[] args) {
     
        String name = null;
        System.out.println(name.length());
    }

//    Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null
//    at WeekDemo02.ExceptionDemo.main(ExceptionDemo.java:6)

}

Java Exception_第6张图片

Java Exception_第7张图片

Java Exception_第8张图片

Java Exception_第9张图片

编译异常

Java Exception_第10张图片

小练习

Java Exception_第11张图片

异常处理

Java Exception_第12张图片

Java Exception_第13张图片

Java Exception_第14张图片

Java Exception_第15张图片

try-catch异常处理

Java Exception_第16张图片

try-catch方法处理异常-注意事项

Java Exception_第17张图片

Java Exception_第18张图片

Java Exception_第19张图片

01:

package TryDemo;

public class TryCatchDetail {
     
    public static void main(String[] args) {
     
        try {
     
            String str = "123";
            int a = Integer.parseInt(str);
            System.out.println("a");
        } catch (NumberFormatException e) {
     
            System.out.println(e.getMessage());
        }

        System.out.println("continue");


//        a
//        continue
    }
}


//================================================================================


package TryDemo;

public class TryCatchDetail {
     
    public static void main(String[] args) {
     
        try {
     
            String str = "HSPEDU";
            int a = Integer.parseInt(str);
            System.out.println("a");//这条代码运行不到
        } catch (NumberFormatException e) {
     
            System.out.println(e.getMessage());
        }

        System.out.println("continue");


//        For input string: "HSPEDU"
//        continue
    }
}

//============================================================================


package TryDemo;

public class TryCatchDetail {
     
    public static void main(String[] args) {
     
        try {
     
            String str = "123";
            int a = Integer.parseInt(str);
            System.out.println("a");
        } catch (NumberFormatException e) {
     
            System.out.println(e.getMessage());
        }finally {
     
            System.out.println("123");
        }

        System.out.println("continue");


//        a
//        123
//        continue
    }
}


//====================================================================================

package TryDemo;

public class TryCatchDetail {
     
    public static void main(String[] args) {
     
        try {
     
            String str = "HSP";
            int a = Integer.parseInt(str);
            System.out.println("a");
        } catch (NumberFormatException e) {
     
            System.out.println(e.getMessage());
        }finally {
     
            System.out.println("123");
        }

        System.out.println("continue");


//        For input string: "HSP"
//        123
//        continue
    }
}





小练习

Java Exception_第20张图片

Java Exception_第21张图片

Java Exception_第22张图片

try-catch-finally执行顺序小结

Java Exception_第23张图片

小练习

Java Exception_第24张图片

throws异常处理

Java Exception_第25张图片

Java Exception_第26张图片

Java Exception_第27张图片

注意事项和使用细节

Java Exception_第28张图片

Java Exception_第29张图片

Java Exception_第30张图片

自定义异常

Java Exception_第31张图片

Java Exception_第32张图片

在这里插入图片描述

01:

package TryCatchExercise;

import SuperWorkDemo.A;

public class CustomException {
     
    public static void main(String[] args) {
     

        int age = 10000;

        if (!(age >= 18 && age <= 120))
        {
     
            throw new AgeException("age need a range of 18——120");
        }
        System.out.println("ok");
        
        //Exception in thread "main" TryCatchExercise.AgeException: age need a range of 18——120
        //	at TryCatchExercise.CustomException.main(CustomException.java:12)
    }
}



//自定义异常
class AgeException extends RuntimeException
{
     
    //构造器
    public AgeException(String message) {
     
        super(message);
    }
}

throw和throws的区别

Java Exception_第33张图片

小练习

Java Exception_第34张图片

Java Exception_第35张图片

大练习

Java Exception_第36张图片

01:

package TryCatchExercise;

public class homeWork {
     
    public static void main(String[] args) {
     
        try {
     
            if (args.length!=2)
            {
     
                throw new ArrayIndexOutOfBoundsException("参数个数不对");
            }

            int n1 = Integer.parseInt(args[0]);
            int n2 = Integer.parseInt(args[1]);

            double res = cal(n1,n2);
            System.out.println(res);
        } catch (ArrayIndexOutOfBoundsException e) {
     
            System.out.println(e.getMessage());
        }
        catch(NumberFormatException e)
        {
     
            System.out.println("参数格式不正确,需要输入整数");
        }
        catch(ArithmeticException e)
        {
     
            System.out.println("出现了除0的异常");
        }
    }


    public static double cal(int n1,int n2)
    {
     
        return n1/n2;
    }
}

02:

Java Exception_第37张图片

Java Exception_第38张图片

03:

Java Exception_第39张图片

04:

Java Exception_第40张图片

你可能感兴趣的:(Java学习之路,java,tomcat,算法,Exception,异常)