java 作业

#千锋逆战班#
在千锋“逆战”学习第 26 天,
今天学习的是异常的相关内容。
中国加油!武汉加油!千锋加油!
学习的脚步不停止!

天道酬勤,继续加油!

Day 26 作业

1.Java中所有的错误都继承自Throwable类 ;在该类的子类中,Error类表示严重的底层错误 ,对于这类错误一般处理的方式是不可处理Exception 类 表示例外、异常。

2.查询API ,填空
I.异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于已检查异常,从处理方式上说,对这种异常必须处理。
II.异常类 java.util.regex.PatternSyntaxException ,从分类上说,该类属于运行时 异常,从处理方式上说,对这种异常可处理可不处理

package com.day26.TestWork;

public class TestThrow {

	public static void main(String[] args) {

		throwException(10);
	}
	public static void throwException(int n) {
		if(n==0) {
		//抛出一个 NullPointerException
			throw new NullPointerException();
		}else {
			//抛出一个 ClassCastException 
			//并设定详细信息为“类型转换出错”
			throw new ClassCastException("类型转换错误");
		}
	}
}

n=1:

main 1
ma1
mb1
Catch EOFException
In finally
main2

n=2:

main 1
ma1
mb1
Catch IOException
In finally
main2

n=3:

main 1
ma1
mb1
Catch SQLException
In finally
main2

n=4:

main 1
ma1
mb1
Catch Exception
In finally
main2

n=5:

main 1
ma1
mb1
mb2
ma2
In finally
main2
package com.day26.TestWork;

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();
	}

}

9.A
10.

package com.day26.TestWork;

public class TestFinally{ 
	public static void main(String args[]){ 
		System.out.println ( ma() ); 
	} 
	public static int ma(){ 
		//int b = 0; 
		int b = 100; 
		try{ 
			int n = 100; 
			return n/b; 
		}catch(Exception e){ 
			return 10; 
		}finally{ 
			return 100; 
		} 
	} 
} 


在 ma 中,当读入的 b 为 100 时,输出结果为 100,当读入的 b 为 0 时,输出结果为100

package com.day26.TestWork;

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 = 0; 
		int b = 0; 
		try{ 
			System.out.println("ma1"); 
			int result = n / b; 
			System.out.println("ma2" + result); 
		}finally{ 
			System.out.println("In Finally"); 
		} 
	} 
} 

在 ma 中,读入整数 b,如果读入的值为 10,则输出:

ma1
ma21
In Finally

如果读入的值为 0,则输出:

ma1
In Finally

13.A
14.AB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值