java数值格式非法异常类_Java 里面的异常

一、异常概述

Exception类是 Throwable 类的子类。它是因编程错误或由于偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。

除了 Exception 类外,Throwable 还有一个子类 Error 。

Error是指运行时环境发生的错误,Java 虚拟机无法解决的严重问题 。

例如,JVM 内存溢出。一般地,程序不会从错误中恢复, 一般不编写针对性

的代码进行处理。

异常类有两个主要的子类:IOException 类和 RuntimeException 类。

Fja3IexUrQ_D8RBjKI6qQEwSL7Vj

(此图片来源于网络)

二、编译时异常

编译时异常也称为受检异常

是指编译器要求必须进行处理的异常。即程序在运行时由于外界因素造成的一般性异常。

package com.zxy.java.exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.IOException;

/**

* @Description: FileNotFoundException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest1 {

public static void main(String[] args) {

FileReader fileReader = null;

try {

// 实例化File对象

File file = new File("test.txt");

// 实例化FileReader流,用于数据的读入

fileReader = new FileReader(file);

// 读入数据

char[] chars = new char[5];

int length;

while ((length = fileReader.read(chars)) != -1) {

String s = new String(chars, 0, length);

System.out.println(s);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

// 不再使用的时候,关闭流资源

if (fileReader != null) {

try {

fileReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

测试结果:

java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(FileInputStream.java:195)

at java.io.FileInputStream.(FileInputStream.java:138)

at java.io.FileReader.(FileReader.java:72)

at com.zxy.java.exception.ExceptionTest1.main(ExceptionTest1.java:21)

三、运行时异常

运行时异常也被称作非受检异常

是指编译器不要求必须处理的异常。一般是指编程时的逻辑错误。

1.空指针异常

当程序运行时,对象未初始化或为空时,将会抛出空指针异常。

package com.zxy.java.exception;

/**

* @Description: NullPointerException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest2 {

public static void main(String[] args) {

String s = "HelloWorld";

s = null;

System.out.println(s.charAt(1));

}

}

测试结果:

Exception in thread "main" java.lang.NullPointerException

at com.zxy.java.exception.ExceptionTest2.main(ExceptionTest2.java:13)

2. 数组下标越界异常

指使用非法索引访问数组。索引为负值或大于或等于数组的大小。将会抛出数组下标越界异常。

package com.zxy.java.exception;

/**

* @Description: ArrayIndexOutOfBoundsException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest3 {

public static void main(String[] args) {

int[] array = new int[5];

System.out.println(array[6]);

}

}

测试结果:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6

at com.zxy.java.exception.ExceptionTest3.main(ExceptionTest3.java:12)

3. 数字格式异常

一般由 Integer.valueOf(String param) 或者 Integer.parseInt(String param) 引起数字异常

package com.zxy.java.exception;

/**

* @Description: NumberFormatException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest4 {

public static void main(String[] args) {

String str = "111";

str = "abc";

int num = Integer.parseInt(str);

System.out.println(num);

}

}

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)

at java.lang.Integer.parseInt(Integer.java:615)

at com.zxy.java.exception.ExceptionTest4.main(ExceptionTest4.java:13)

4. 类型转换异常

当程序试图将对象强制转换为不是该实例的子类时,将会抛出类型转换异常

package com.zxy.java.exception;

import java.util.Date;

/**

* @Description: ClassCastException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest5 {

public static void main(String[] args) {

Object obj = new Date();

String str = (String) obj;

}

}

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String

at com.zxy.java.exception.ExceptionTest5.main(ExceptionTest5.java:14)

5. 算术异常

当程序中出现异常的运算条件时,将抛出算术异常。例如,一个整数“除以零”时,抛出算术异常。

package com.zxy.java.exception;

import java.util.Date;

/**

* @Description: ArithmeticException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest6 {

public static void main(String[] args) {

int m = 8;

int n = 0;

System.out.println(m / n);

}

}

Exception in thread "main" java.lang.ArithmeticException: / by zero

at com.zxy.java.exception.ExceptionTest6.main(ExceptionTest6.java:15)

6. 输入不匹配异常

在使用scanner进行控制台输入的时候,使用next()方法输出,这种方法有一定的弊端。比如你使用nextInt()方法,但是输入一个字符串,就会抛出输入不匹配异常。

package com.zxy.java.exception;

import java.util.Scanner;

/**

* @Description: InputMismatchException

* @Author: zhangxy

* @Date: Created in 2019/11/20

* @Modified By:

*/

public class ExceptionTest7 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int wage = scanner.nextInt();

System.out.println(wage);

}

}

aaa

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:864)

at java.util.Scanner.next(Scanner.java:1485)

at java.util.Scanner.nextInt(Scanner.java:2117)

at java.util.Scanner.nextInt(Scanner.java:2076)

at com.zxy.java.exception.ExceptionTest7.main(ExceptionTest7.java:14)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值