一、實(shí)驗(yàn)預(yù)熱
1腕够、請(qǐng)解釋編譯時(shí)異常和運(yùn)行時(shí)異常的區(qū)別级乍,并舉出幾個(gè)常見的編譯時(shí)異常和運(yùn)行時(shí)異常
1)編譯時(shí)異常是在編寫程序是就會(huì)發(fā)現(xiàn)的異常,例如:IOException帚湘、FileNotFoundException玫荣、SQLException、NoSuchMethodException大诸、ClassNotFoundException
2)運(yùn)行時(shí)異常是程序在運(yùn)行期間發(fā)現(xiàn)的異常捅厂,例如:ArrayIndexOutOfBoundsException、ArithmeticException资柔、IllegalArgumentException焙贷、ClassCastException、NumberFormatException建邓、NullPointerException
2盈厘、請(qǐng)說出throw和throws的區(qū)別
Throw是拋出異常實(shí)例化對(duì)象(一定拋出異常)
Throws是拋出異常聲明(可能拋出異常)
二、實(shí)驗(yàn)內(nèi)容
1官边、自行完成一個(gè)簡(jiǎn)單的自定義異常并處理沸手,使程序正確執(zhí)行,并獲得異常返回信息注簿。
代碼:
package leif.tests;
public class ExperimentalReport {
public static void main(String[] args) {
try {
throw new ServiceException("ServiceException");
} catch (ServiceException serviceException) {
System.out.println(serviceException.getMessage());
System.out.println(serviceException.getServiceExceptionMessage());
serviceException.printStackTrace();
}
}
}
class ServiceException extends Exception {
private static final long serialVersionUID = 1L;
private String serviceExceptionMessage;
public ServiceException(String serviceExceptionMessage) {
super("Exception");
this.serviceExceptionMessage = serviceExceptionMessage;
}
public String getServiceExceptionMessage() {
return serviceExceptionMessage;
}
public void setServiceExceptionMessage(String serviceExceptionMessage) {
this.serviceExceptionMessage = serviceExceptionMessage;
}
}
結(jié)果截圖:
2契吉、在程序中實(shí)現(xiàn)兩個(gè)數(shù)字求商的功能,當(dāng)除數(shù)為0時(shí)诡渴,正確使用try-catch-finally捕捉如圖所示的程序異常捐晶。
代碼:
package leif.tests;
import java.util.Scanner;
public class ExperimentalReport {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請(qǐng)輸入被除數(shù):");
int dividend = scanner.nextInt();
System.out.print("請(qǐng)輸入除數(shù):");
int divisor = scanner.nextInt();
try {
if (!isInterger(dividend) || !isInterger(divisor) || isZero(divisor)) {
throw new ArithmeticException("出現(xiàn)錯(cuò)誤:被除數(shù)和除數(shù)必須是整數(shù)菲语,且除數(shù)不能為零。");
}
System.out.println(dividend / divisor);
} catch (ArithmeticException arithmeticException) {
arithmeticException.printStackTrace();
} finally {
scanner.close();
}
}
public static boolean isInterger(Object object) {
if (object instanceof Integer) {
return true;
} else {
return false;
}
}
public static boolean isZero(int i) {
if (i == 0) {
return true;
} else {
return false;
}
}
}
結(jié)果截圖:
3惑灵、在程序中編寫方法實(shí)現(xiàn)計(jì)算兩數(shù)的商山上,該方法使用throws聲明異常,在該方法的調(diào)用處使用try-catch-finally進(jìn)行異常捕捉英支。
代碼:
package leif.tests;
import java.util.Scanner;
public class ExperimentalReport {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請(qǐng)輸入被除數(shù):");
int dividend = scanner.nextInt();
System.out.print("請(qǐng)輸入除數(shù):");
int divisor = scanner.nextInt();
try {
getResult(dividend, divisor);
} catch (ArithmeticException arithmeticException) {
arithmeticException.printStackTrace();
} finally {
scanner.close();
}
}
public static int getResult(int dividend, int divisor) throws ArithmeticException {
return dividend / divisor;
}
}
結(jié)果截圖:
4佩憾、正確使用throw拋出異常,實(shí)現(xiàn)當(dāng)輸入的性別不是“男”或“女”時(shí)干花,顯示如下圖所示的程序異常效果妄帘。
代碼:
package leif.tests;
import java.util.Scanner;
public class ExperimentalReport {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請(qǐng)輸入性別:");
String gender = scanner.next();
try {
if (!"男".equals(gender) && !"女".equals(gender)) {
throw new Exception("性別必須是“男”或“女”!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}
結(jié)果截圖: