附录:
题目1代码:
public class TryArithmeticException { /** * @param args */ static int a=1; static int b=0; public static void main(String[] args) { // TODO Auto-generated method stub try { int c=a/b; System.out.print(c); } catch(ArithmeticException e) { System.out.println(\ System.out.println(\ } } }
题目2代码:
public class TryThrow { /** * @param args */ static int[] a=new int[6]; static void methodOne() throws ArrayIndexOutOfBoundsException { throw new ArrayIndexOutOfBoundsException(\数组下标越界\ } public static void main(String[] args) { // TODO Auto-generated method stub try{ methodOne(); } catch(ArrayIndexOutOfBoundsException e ){ System.out.println(\错误是:\ }
} }
题目3代码:
import java.util.Scanner;
@SuppressWarnings(\
class InterException extends Exception{ String message; public InterException(double m){ message= \输入的成绩\不合法\请检查您的输入是否有误!\ } public String toString(){ return message; } }
public class TryRecord { int n1,n2; double sum=0; public void setRecord(double record) throws InterException { if(record>100||record<0){ throw new InterException(record); } else{ if(record>=60) { n1++; } else{ n2++; } sum+=record; } } public static void main(String[] args) { TryRecord record=new TryRecord(); @SuppressWarnings(\ Scanner reader=new Scanner(System.in);
System.out.print(\请输入班级总人数:\ int n=reader.nextInt();//班级总人数 System.out.println(\请您逐次输入学生课程成绩:\ for(int i=0;i 题目4代码: Bank.java: public class Bank { /** * @param args */ double balance; double dAmount; public Bank(double balance){ this.balance=balance; } public Bank() { // TODO Auto-generated constructor stub } void deposite(double dAmount){ if(dAmount>0) {this.dAmount=dAmount; this.balance+=dAmount;} } void withdrawal(double dAmount) throws InsufficientFundsException{ //System.out.println(\取款金额为:\ if(dAmount>this.balance){ throw new InsufficientFundsException(this.balance,dAmount); } else{ this.balance-=dAmount;} } double show_balance(){ return this.balance; } public static void main(String[] args) { // TODO Auto-generated method stub } } InsufficientFundsException.java: @SuppressWarnings(\ public class InsufficientFundsException extends Exception { /** * @param args */ double balance; double dAmount; public InsufficientFundsException(double balance,double dAmount){ this.balance=balance; this.dAmount=dAmount; } String excepMesagge(){ Bank bank=new Bank(this.balance); return \您的取款金额为\但是账户余额仅为\操作不合法!\ } public static void main(String[] args) { // TODO Auto-generated method stub } } ExceptionDemo.java: public class ExceptionDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Bank bank=new Bank(2000); bank.deposite(100); try{ bank.withdrawal(4000); } catch(InsufficientFundsException e){ System.out.println(\ System.out.println(e.excepMesagge()); } finally{ System.out.println(\操作退出!\ } } }