Java Exception

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Exception相关的知识,希望对你有一定的参考价值。

Exception

异常捕获

  • 将代码块选中->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");
    }
}

异常介绍

基本概念

异常体系图和小结

常见的运行时异常

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)

}

编译异常

小练习

异常处理

try-catch异常处理

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

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
    }
}





小练习

try-catch-finally执行顺序小结

小练习

throws异常处理

注意事项和使用细节

自定义异常

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的区别

小练习

大练习

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:

03:

04:

以上是关于Java Exception的主要内容,如果未能解决你的问题,请参考以下文章

[未解决问题记录]python asyncio+aiohttp出现Exception ignored:RuntimeError('Event loop is closed')(代码片段

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'dll

方法

异常和TCP通讯

java中error和exception有啥区别

java:Zip Exception无效代码长度?