千锋逆战班,Day26

在千锋逆战学习的第26天
天道酬勤!加油!
今天学习了异常的概念、分类和处理

作业题:

在这里插入图片描述
答:Throwable,Error,不应该试图抓住它,Exception
2.
在这里插入图片描述
答:已检查,必须,运行时,可处理可不处理
3.
在这里插入图片描述

public class TestThrow {

	public static void main(String[] args) {
		throwException(10);
	}

	public static void throwException(int n) {
		if (n == 0) {
			throw new NullPointerException();
		} else {
			throw new ClassCastException("类型转换异常");
		}
	}
}
import java.io.*;
import java.sql.SQLException;

public class TestException {

	public static void main(String[] args) {

		System.out.println("main 1");
		int n;
		// 读入n
		ma(n);
		System.out.println("main2");

	}

	public static void ma(int n) {
		try {
			System.out.println("ma1");
			mb(n);
			System.out.println("ma2");
		} catch (EOFException e) {
			System.out.println("Catch EOFException");
		} catch (IOException e) {
			System.out.println("Catch IOEException");
		} catch (SQLException e) {
			System.out.println("Catch SQLException");
		} catch (Exception e) {
			System.out.println("Catch Exception");
		} finally {
			System.out.println("In finally");
		}
	}

	public static void mb(int n) throws Exception {
		System.out.println("mb1");
		if (n == 1)
			throw new EOFException();
		if (n == 2)
			throw new FileNotFoundException();
		if (n == 3)
			throw new SQLException();
		if (n == 4)
			throw new NullPointerException();
		System.out.println("mb2");

	}
}
//问当读入的n分别为1,2,3,4,5时,输出的结果分别是什么

当n=1时
在这里插入图片描述
n=2时
在这里插入图片描述
n=3时
在这里插入图片描述
n=4时
在这里插入图片描述
n=5时
在这里插入图片描述
7.代码改错

class MyException{}
public class TestException {

	public static void main(String[] args) {

		ma();
	}

	public static int ma(){
		try{
			m();
			return 100;
		}catch(Exception e){
			System.out.println("Exception");
		}catch(ArithmeticException e){
			System.out.println("ArithmeticException");
		}
	}
	public static void m(){
		throw new MyException();
	}
}

修改后

class MyException extends Exception {
	public MyException() {
		super();
	}

	public MyException(String msg) {
		super(msg);
	}
}

public class TestException {

	public static void main(String[] args) {

		ma();
	}

	public static int ma() {
		try {
			m();
			return 100;
		} catch (ArithmeticException e) {
			System.out.println("ArithmeticException");
		} catch (Exception e) {
			System.out.println("Exception");
		}
		return 0;
	}

	public static void m() throws MyException {
		throw new MyException();
	}
}

在这里插入图片描述
答:A
11.写出下面代码的运行结果

public class TestTryFinally {

	public static void main(String[] args) {

		try{
			ma();
		}catch(Exception ex1){
			
		}
		
	}
	
	public static void ma() throws Exception{
		int n = 10;
		int b;
		//读入一个整数b
		try{
			System.out.println("ma1");
			int result = n/b;
			System.out.println("ma2"+ result);
		}finally{
			System.out.println("In Finally");
		}
	}
}
//在ma中,读入整数b,如果读入的值为10,则输出_______。
//如果读入的值为0,则输出________。

答:
在这里插入图片描述
在这里插入图片描述
13.有以下代码

public class TestException {

	public static void main(String[] args) {

		try {
			System.out.println("main1");
			ma();
			System.out.println("main2");
		} catch (Exception e) {
			System.out.println("In Catch");
		}
	}

	public static void ma() {
		System.out.println("ma1");
		throw new NullPointerException();
		System.out.println("ma2");
	}
}

在这里插入图片描述
答:A
14.
在这里插入图片描述
答:AB

发布了24 篇原创文章 · 获赞 1 · 访问量 721

猜你喜欢

转载自blog.csdn.net/weixin_46286064/article/details/104759308
今日推荐