700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 复习java基础第二天(异常处理)

复习java基础第二天(异常处理)

时间:2021-12-24 07:23:21

相关推荐

复习java基础第二天(异常处理)

一、常见的异常类型:

public class TestException {public static void main(String[] args) {int i = 10;//数学异常: java.lang.ArithmeticException int j = i / 0; System.out.println(j); int [] scores = new int[]{1, 2, 4, 5};//数组下标越界异常: java.lang.ArrayIndexOutOfBoundsException System.out.println(scores[4]); Person p1 = new Man();//类型转换异常: java.lang.ClassCastException Woman p2 = (Woman) p1; p1 = null;//空指针异常: java.lang.NullPointerException System.out.println(p1.toString()); System.out.println("end...");} }class Person{}class Man extends Person{}class Woman extends Person{}

二、不论在try、catch代码块中是否发生了异常事件,finally块中的语句都会被执行。

import java.io.FileNotFoundException;import java.io.InputStream;public class TestTryCatchFinally {public static void main(String[] args) {try {int i = 10;int j = i / 0;} finally{System.out.println("finally...");}//不论在try、catch代码块中是否发生了异常事件,finally块中的语句都会被执行。 System.out.println("end...");//示例编译时异常, IO 异常属于编译时异常. try {InputStream is = new FileInputStream("abc.txt");} catch (FileNotFoundException e) {e.printStackTrace();}} }

三、throws:

1. 在 Java 中使用 throws 关键字声明抛出异常.

2. throws 方法抛出的异常可以是方法中出现的异常的类型或其父类类型.

3. throws 可以声明抛出多个异常, 多个异常使用 , 分割.

4. 运行时异常不需要使用 throws 关键字进行显式的抛出.

5. 重写方法不能抛出比被重写方法范围更大的异常类型.

import java.io.FileNotFoundException;import java.io.IOException;public class TestThrows {public static void main(String[] args) {try {test();} catch (Exception e) {e.printStackTrace();}}public static void test(){ int i = 10 / 0;System.out.println(i);// InputStream fs = new FileInputStream("abc.txt"); // Connection connection = null;// String sql = null;// PreparedStatement ps = connection.prepareStatement(sql);// byte [] buffer = new byte[fs.available()];// fs.read(buffer); A a = new B();try {a.method();} catch (FileNotFoundException e) {e.printStackTrace();} } }class A{void method () throws FileNotFoundException{ }}class B extends A{// @Override// void method() throws IOException {// }}

四:throw:

人工手动抛出异常:

1. 创建一个异常类对象

2. 在方法内部使用 throw 关键字把该异常类对象抛出去!

自定义的异常类:

1. 通常继承自 RuntimeException(可以继承 Exception)

2. 自定义的异常类就是用来被人工抛出的!

import java.util.Scanner;public class TestThrow {public static void main(String[] args) {try {inputAge();} catch (Exception e) {System.out.println(e.getMessage()); }System.out.println("end...");}/*** 输入年纪: 要求年纪必须在 15-30 之间, 超出 30 则年纪偏大*/public static void inputAge(){Scanner sc = new Scanner(System.in); System.out.print("age=");int age = sc.nextInt();if(age > 30){//System.out.println("年纪偏大.");throw new AgeTooLargeException("年纪偏大.");}}public static void test(){//1. 创建一个异常类对象RuntimeException ex = new RuntimeException();//2. 把异常类对象抛出去throw ex;} }

五、自定义异常举例:

1、先定义一个异常类:

public class EcDef extends RuntimeException{public EcDef() {// TODO Auto-generated constructor stub }public EcDef(String msg) {super(msg);}}

2、再创建一个处理异常的类:

public class EcmDef {public static void main(String[] args) {try {int i = Integer.parseInt(args[0]); // "a"int j = Integer.parseInt(args[1]);System.out.println(ecm(i, j));} catch (ArrayIndexOutOfBoundsException e) {System.out.println("输入的参数个数不足.");} catch (ArithmeticException e) {System.out.println("除数不能为 0");} catch (EcDef e) {System.out.println(e.getMessage()); } catch (NumberFormatException e) {System.out.println("输入的参数不能转为整型.");}}public static int ecm(int i, int j){if(i < 0 || j < 0){throw new EcDef("不能处理负数. ");}int result = i / j;return result;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。