1.程序中凡是可能出现异常的地方必须进行捕获或拋出”,这句话对吗?
不对。
异常类型是RuntimeException或是其子类,程序方法可以对异常不作任何声明抛出或处理,直接交给调用该方法的地方处理,程序能编译通过,不会对可能产生异常的代码行给出提示。
2.自定义一个异常类,并在程序中主动产生这个异常类对象
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();}
}
}
3.借助JDK帮助,请列举发生NullPointerException异常的一些情况
当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:
调用 null 对象的实例方法。
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
4.不执行程序,指出下面程序的输出结果;如果将黑体代码去掉,写出输出结果;如果再将斜体代码去掉,写出输出结果。
public class TestShape {
public static void aMethod() throws Exception{
try{
throw new Exception();
}
//------------------黑体------------------------------------------------
catch(Exception e){
System.out.println("exception000");
}
//----------------------------------------------------------------------
//--------------------斜体-----------------------------------------------
finally{
System.out.println("exception111");
}
//---------------------------------------------------------------------------
}
public static void main(String[] args){
try{
aMethod();
}
catch(Exception e){
System.out.println("exception");
}
System.out.println("finished");
}
}
exception000
exception111
finished
去掉黑体代码:
exception111
exception
finished
去掉斜体代码:
exception000
finished
本题关键是aMethod函数是否已经进行异常捕获(已经进行了try catch操作),进行了则在第二个catch中则不再进行,否则如果第一个未进行catch则第二个catch语句毕竟进行
5.不执行程序,指出下面程序的输出结果。
public class Test{
public static String output ='';
public static void foo(int i){
try{
if(i==1) {throw new Exception();}
output += "1";
}
catch(Exception e){
output += "2";
return;
}
finally{output += "3";}
output += "4";
}
public static void main(String args[]){
foo(0);
foo(1);
System.out.println(Test.output);
}
}
输出:13423
代码说明:foo(0)进入到try中的output+="1"——>finally{output+='3'}——>output+='4';
foo(1)进入到try中if(i==1),抛出异常,后面语句不再执行——>catch(){output+='2'},return 的结果被暂存——>finally{output+='3'}——>return
此问题的关键在于,如果出现异常则执行 try-catch-finally全套,例如output+=“4”则不会执行
6.编写一个程序方法,对空指针异常、除数为零异常给出出错的中文提示。当有新异常发生时,可扩展该方法中的代码进行统一处理。
public class Test{
public static void main(String[] args) {
try {
String s = null;
//System.out.println(1/0);//除零异常
System.out.println(s.charAt(0));//空指针异常
}catch (NullPointerException e) {
System.out.println("空指针异常");
}catch (ArithmeticException e) {
System.out.println("计算异常");
}catch (Exception e) {
System.out.println("其他异常");
e.printStackTrace();
}
}
7.从屏幕输入10个数,在输入错误的情况下,给出相应的提示,并继续输人。在输入完成的情况下,找到最大最小数。
import java.util.Scanner;
public class TestShape {
private static Scanner sc;
public static void main(String[] args) {
int index = 0;
int[] array = new int[10];
int max,min;
while (true) {
if (index == array.length) { break;}
sc = new Scanner(System.in);
System.out.print("输入" + (index + 1) + ":");
try {
array[index] = sc.nextInt();
index++;
}
catch (Exception e) {System.out.println("输入错误,重新输入!");}
}
max = array[0];
min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {max = array[i];}
if (array[i] < min) {min = array[i];}
}
System.out.println("max = " + max);
System.out.println("min = " + min);
}
}
8.阅读下面程序,TimedOutException为自定义异常,完成指定方法后面的部分。
public void method()throws TimedOutException
{
success= connect();
if(success == -1)
{
throw new TimedOutException();
}
}