the try { // program code } the catch (exceptionName E1) { // the Catch block }
import java.io.*; public class ExcepTest{ public static void main(String args[]){ try{ int a[] = new int[2]; System.out.println("Access element three :" + a[3]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } System.out.println("Out of the block"); } }
the try { // program code } the catch (Exception variable name of a type of abnormality) { // program code } the catch (Exception Type 2 abnormality variable name 2) { // program code } the catch (Exception Type 2 abnormality variable name 2) { // program code }
try { file = new FileInputStream(fileName); x = (byte) file.read(); } catch(FileNotFoundException f) { // Not valid! f.printStackTrace(); return -1; } catch(IOException i) { i.printStackTrace(); return -1; }
import java.io.*; public class className { public void deposit(double amount) throws RemoteException { // Method implementation throw new RemoteException(); } //Remainder of class definition }
import java.io.*; public class className { public void withdraw(double amount) throws RemoteException, InsufficientFundsException { // Method implementation } //Remainder of class definition }
the try { // program code } the catch (Exception Type 1 abnormality variable name) { // program code } the catch (Exception Type 2 abnormality variable name 2) { // program code } the finally { // program code }
public class ExcepTest{ public static void main(String args[]){ int a[] = new int[2]; try{ System.out.println("Access element three :" + a[3]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } finally{ a[0] = 6; System.out.println("First element value: " +a[0]); System.out.println("The finally statement is executed"); } } }
class MyException extends Exception{ }
Import the java.io. * ; // custom exception class inherits the Exception class public class the InsufficientFundsException the extends Exception { // (time taken to balance money) amount when herein used to store abnormal lack money Private Double AMOUNT ; public the InsufficientFundsException ( Double AMOUNT) { the this .amount = AMOUNT; } public Double getAmount () { return AMOUNT; } }
Import the java.io. * ; // Such simulations bank account public class the CheckingAccount { // Balance is the balance, number of card Private Double Balance; Private int Number; public the CheckingAccount ( int Number) { the this .number = Number; } / / method: save public void Deposit ( Double AMOUNT) { Balance + = AMOUNT; } // methods: withdrawals public void the withdraw (double amount) throws InsufficientFundsException { if(amount <= balance) { balance -= amount; } else { double needs = amount - balance; throw new InsufficientFundsException(needs); } } //方法:返回余额 public double getBalance() { return balance; } //方法:返回卡号 public int getNumber() { return number; } }
public class BankDemo { public static void main(String [] args) { CheckingAccount c = new CheckingAccount(101); System.out.println("Depositing $500..."); c.deposit(500.00); try { System.out.println("\nWithdrawing $100..."); c.withdraw(100.00); System.out.println("\nWithdrawing $600..."); c.withdraw(600.00); }catch(InsufficientFundsException e) { System.out.println("Sorry, but you are short $" + e.getAmount()); e.printStackTrace(); } } }