1. Throwable的子类包含哪两类?简述Java Error类与Exception类的区别。
包含Error类与Exception类
区别:Error是所有错误类的祖先类,Exception是所有异常类的祖先类。前者不是程序需要捕获与进行处理的,当Error发生时,程序会立即停止
2. Exception又分为checked异常和unchecked异常,请分别举例说明。
派生于Error或者RuntimeException的异常称为unchecked异常,通过使用try - catch - [finally]语句块,用来对可能产生异常的代码产生的异常进行捕获,并根据其异常类型进行不同的操作。所有其他的异常称为checked异常,如public static void main(String args[]) throws IOExceprion。
3. 请查阅资料,简述StackOverflowError和OutOfMemoryError两类错误的发生情形和原因。
1、线程请求的栈深度大于虚拟机所允许的深度,将抛出StackOverflowError 异常
—| 递归可能造成StackOverflowError
—| 不断创建线程可能造成StackOverflowError
2、栈的深度(大小类似于弹夹深度)可以自动扩展,扩展时无法申请到足够的内存时会抛出OutOfMemoryError异常
3、虚拟机栈一样,本地方法栈区域也会抛出StackOverflowError 和OutOfMemoryError
异常。
4、如果在堆中没有内存完成实例分配,并且堆也无法再扩展时,将会抛出OutOfMemoryError 异常
5、当方法区无法满足内存分配需求时,将抛出OutOfMemoryError 异常。
6、当常量池(方法区的一部分)无法再申请到内存时会抛出OutOfMemoryError 异常
7、各个内存区域的总和大于物理内存限制(包括物理上的和操作系统级的限制),从而导致动态扩展时出现OutOfMemoryError异常。
4. 简述异常处理的两种方式,并举例说明区别。
这两种方式分别为声明抛出处理与程序捕获处理。而声明抛出处理又分为隐式声明抛出与显式声明抛出;程序捕获处理则分为非嵌套与嵌套。
隐式声明抛出:程序方法对这类异常不作任何声明抛出或者处理,直接交给调用该方法的地方处理,且程序并不会编译失败也不会对产生异常的代码给出提示。
显式声明抛出:显式地通过throws向上级抛出异常。例如public static void main(String args[]) throws IOExceprion;
程序捕获处理:
通过使用try - catch - [finally]语句块,用来对可能产生异常的代码产生的异常进行捕获,并根据其异常类型进行不同的操作。
5. 选取RuntimeException类的五个子类,编写抛出并捕获上述子类异常的程序。(例如算术异常,空指针异常,类转换异常,数组越界异常等)
import java.util.EmptyStackException;
import java.util.Stack;
class A{
int v = 6;
public int getV() {
return v;
}
}
public class ExcpOp {
public static void Arithmetic() {
int a = 6, b = 0;
try{
int c = a / b;
} catch (ArithmeticException ae) {
System.out.println(ae.getClass().getName()+" has been throw");
} finally {
System.out.println("ArithmeticEp is over!\n");
}
}
public static void NullPointer() {
try {
A a = null;
a.getV();
} catch (NullPointerException npe) {
System.out.println(npe.getClass().getName()+" has been throw");
} finally {
System.out.println("NullPointer is over!\n");
}
}
public static void EmptyStack() {
Stack s = new Stack();
try{
s.push(5);
s.pop();
System.out.println("Pop 1");
s.pop();
System.out.println("Pop 2");
} catch (EmptyStackException ese) {
System.out.println(ese.getClass().getName()+" has been throw");
} finally {
System.out.println("EmptyStack is over!\n");
}
}
public static void IndexOutOfBounds() {
int[] a = new int[3];
for (int i = 0; i<3 ; i++ ) {
a[i] = i;
}
try{
System.out.println(a[4]);
} catch (IndexOutOfBoundsException ioe) {
System.out.println(ioe.getClass().getName()+" has been throw");
} finally {
System.out.println("EmptyStack is over!\n");
}
}
public static void NegativeArraySize() {
try{
int[] a = new int[-3];
} catch (NegativeArraySizeException nase) {
System.out.println(nase.getClass().getName()+" has been throw");
} finally {
System.out.println("NegativeArraySize is over!\n");
}
}
public static void main(String[] args) {
ExcpOp.Arithmetic();
ExcpOp.EmptyStack();
ExcpOp.IndexOutOfBounds();
ExcpOp.NegativeArraySize();
ExcpOp.NullPointer();
}
}
6. 根据某业务场景自定义一个异常类,并在某场景下抛出该异常对象。
public class MyException extends Exception{
MyException(String msg) {
super(msg);
}
public static void Throw(int a) throws MyException {
if(a !=123456) {
throw new MyException(" 错误 ");
}
}
public static void main(String[] args) {
int a = 660;
try{
MyException.Throw(a);
} catch (MyException me) {
me.printStackTrace();
a = 123456;
} finally {
System.out.println("此时 正确");
}
}
}
7. 异常中的throws声明与throw语句的区别是什么?请举例说明。
1、throw:指的是语句抛出一个异常。
private void checkIndex(int index) {
if (index <= size - 1 && index >= 0) {
} else {
throw new IndexOutOfBoundsException("index:" + index + " size:"
+ size);
}
}
2、throws:指的是方法可能会抛出的异常的声明。
public static void fileCopy(String from, String to) throws IOException {
File fromPath = new File(from);
File toPath = new File(to);
fileCopy(fromPath, toPath);
}
8. finally子句的作用是什么?
如果try中的异常没有在exception中被指出,那么系统将会抛出Traceback(默认错误代码),并且终止程序,接下来的所有代码都不会被执行,但如果有Finally关键字,则会在程序抛出Traceback之前(程序最后一口气的时候),执行finally中的语句。这个方法在某些必须要结束的操作中颇为有用,如释放文件句柄,或释放内存空间等。