java第七章

 1. Throwable的子类包含哪两类?简述Java Error类与Exception类的区别。

Error:
致命 异常,标识系统发生了不可控的错误。程序无法处理,只能人工介入。例如,虚拟机产生了StackOverflowError,OutOfMemoryError。

Exception:
非致命 异常。程序可处理。分为受编辑器检测的checked异常(受检异常)和不受编辑器检测的unchecked异常(非受检异常)。

 2. Exception又分为checked异常和unchecked异常,请分别举例说明。

checked异常:在编译时需要检查的异常,需要用try-catch或throws处理。

import java.io.*;
class Example {  
   public static void main(String args[]) 
   {
	FileInputStream fis = null;
	/*This constructor FileInputStream(File filename)
	 * throws FileNotFoundException which is a checked
	 * exception*/
        fis = new FileInputStream("B:/myfile.txt"); 
	int k; 
 
	/*Method read() of FileInputStream class also throws 
	 * a checked exception: IOException*/
	while(( k = fis.read() ) != -1) 
	{ 
		System.out.print((char)k); 
	} 
 
	/*The method close() closes the file input stream
	 * It throws IOException*/
	fis.close(); 	
   }
}

输出结果

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException

unchecked异常:不需要在编译时处理的异常。在java中派生于Error和RuntimeException的异常都是unchecked exception。

class Example {  
   public static void main(String args[])
   {
	int num1=10;
	int num2=0;
	/*Since I'm dividing an integer with 0
	 * it should throw ArithmeticException*/
	int res=num1/num2;
	System.out.println(res);
   }
}

3. StackOverflowError和OutOfMemoryError两类错误的发生情形和原因。

StackOverFlowError:
原因:堆栈溢出错误一般是递归调用。

package 1;
public class StackOverflowTest {
    public static void main(String[] args) {
        method();
    }
    public static void method(){
        for(;;)
            method();
    }
}

在这里插入图片描述

 

4. 异常处理的两种方式,并举例说明区别。

在这里插入图片描述

 

 
声明抛出处理:
声明抛出 throws 声明抛出的位置:是在方法声明的位置上使用throws关键字向上抛出异常。

程序捕捉处理:
try…catch…

语法:
try{

可能出现异常的代码;

}catch(异常类型1 变量){

处理异常的代码;

}catch(异常类型2 变量){

处理异常的代码;

}…

5. 选取RuntimeException类的五个子类,编写抛出并捕获上述子类异常的程序。(例如算术异常,空指针异常,类转换异常,数组越界异常等)

1.算数异常

int a=5/0;
一个整数“除以零”时,抛出ArithmeticException异常。

2.空指针异常

String s=null;
int size=s.length();
当应用程序试图在需要对象的地方使用 null 时,抛出NullPointerException异常

3.类转异常

Object x = new Integer(0);
System.out.println((String)x);

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

4.数组越界异常

int[] x= new int[2];
System.out.println(x[4]);

如果应用程序试图用超过数组大小的下标,则抛出ArrayIndexOutOfBoundsException异常

5.字符串索引异常

"hello".indexOf(-1);
指示索引或者为负,或者超出字符串的大小,抛出StringIndexOutOfBoundsException异常

 6. 根据某业务场景自定义一个异常类,并在某场景下抛出该异常对象。

public class SelfGenerateException extends Exception
{
	SelfGenerateException(String msg){
		super(msg);   //调用Exception的构造方法
	}
	static void throwOne() throws SelfGenerateException
	{
		int a = 1;
		if (a==1) //如果a为1就认为在特定应用下存在异常,改变执行路径,抛出异常
		{throw new SelfGenerateException("a为1");}		
	}
	public static void main(String args[])
	{
		try
		{throwOne();}
		catch(SelfGenerateException e)
	    {e.printStackTrace();}
	}
}

7. 异常中的throws声明与throw语句的区别是什么?请举例说明。

1、throw:指的是语句抛出一个异常。
例如:如下写了一个抛出异常的语句。利用throw可以自定义异常。
例子:

	private void checkIndex(int index) {
		if (index <= size - 1 && index >= 0) {
		} else {
			throw new IndexOutOfBoundsException("index:" + index + "  size:"
					+ size);
		}
	}

2、throws:指的是方法可能会抛出的异常的声明。
例如:下面的代码可能会抛出IO异常。
例子:

/**
	 * 文件拷贝
	 * 
	 * @param fromPath
	 * @param toPath
	 * @throws IOException
	 */
	public static void fileCopy(String from, String to) throws IOException {
		// 1、建立联系 file对象。源:存在且为文件+目的地:文件可以不存在
		File fromPath = new File(from);
		File toPath = new File(to);
		fileCopy(fromPath, toPath);
	}

 

8. finally子句的作用是什么?

finally常用来处理java一些后续的工作。

在java的的finally首先必须使用在所有catch的最后位置,其次它是必须执行的,无条件执行,甚至即使前面的try-catch语句中已经存在异常了,它仍然会执行。

不管try语句块正常结束还是异常结束,finally语句块是保证要执行的.如果try语句块正常结束,那么在try语句块中的语句都执行完之后,再执行finally语句块。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值