BankException extends Exception { public BankException {} public BankException(String msg) {super(msg);} } class BadValueException



Pobieranie 18.35 Kb.
Data07.05.2016
Rozmiar18.35 Kb.
M2: class Vector {

private int v[];

public Vector(int size) {v=new int [size];}

public void set(int i, int c) {v[i]=c; }

};

M1: Vector v(10); v.set(0, 0);



class BankException extends Exception {

public BankException() {}

public BankException(String msg) {super(msg);}

}

class BadValueException extends BankException {



private int value;

public int getValue() {return value;}

public BadValueException(int v) {value=v;}

public BadValueException(String msg, int v) {super(msg); value=v;}

public String toString() { return super.toString()+” value: “+value;}

}

class DebetException extends BadValueException {



public DebetException(int v) { super(v);}

public DebetException(String msg, int v) {super(msg,v);}

}

class NegativeValueException extends BadValueException {



public NegativeValueException(int v) { super(v);}

public NegativeValueException(String msg, int v) {super(msg,v);}

}
class Account {

private int saldo;

private String name;

public void getMoney(int amount) {

if (amount < 0) throw new NegativeValueException(name, amount);

if (amount > saldo) throw new DebetException(name, amount-saldo);

saldo -= amount;

}

}


class Bank {

private Account accounts[];

public void draw(String accountName) {

Account account;

… account = accounts[i];

try {


account.getMoney(amount);

}

catch (NegativeValueException e) {



Sytem.out.println(“Negative value ?? ”+e.getValue());

}

catch (DebetException e) {



Sytem.out.println(“Mission impossible ”+e.getValue());

}

catch (BankException e) {



System.out.println(“Inny problem z bankiem”);

}

catch (Exception e){e.printStackTrace());}



}

}

class Clerk {



public void draw(Account account, int amount) {

try { acount.getMoney(amount); }

catch(DebetException e) {

if ((e.getValue()<500) && …) account.debetAllowedGetMoney(amount);

else throw e;

}

}



}

void Account.debetAllowedGetMoney(int amount) {saldo-=amount; }

class Bank {

Clerk clerks[];

Account acounts[];

public void draw (int amount) {

try {


clerks[i].draw(accounts[j], amount);

}

catch (DebetException e) {...}



}

}

class E1 extends Exception {}



class E2 extends Exception {}

class F1 extends E1 {}

class A {

A() throws F1 {}

A(int) { }

void f() {}

void g() throws E1 { }

}

class B extends A {



*** B() {} BŁĄD

B() throws F1 {} //powtórzenia OK

B() throws E1, E2 {} //rozszerzenia OK

*** B() throws E2 {} //nie z tej hierarchii

B() {super(0); } //OK. !!!

*** B() {try {super();} catch(F1 f) {} } //BŁĄD, bo super musi być pierwsze

*** void f() throws E1 {} BŁĄD

void f() {} //nie może nic wyrzucić

void g() throws E1 {} //powtórzenie OK.

void g() {} //zawężenie OK.

void g() throws F1 {} //uszczegółowienie OK.

*** void f() throws E2 {} //BŁĄD – zmiana

}

void f() throws Exc1 { … throw new Exc1();….}



void cleanup() throws Exc2 { … throw new Exc2(); ….}

main() throws Exception {



try { … f(); }

catch (Exc2 e) { … } //tu Exc1 jeszcze nie obsłużony !!!



finally{ … cleanup(); }

}




©absta.pl 2019
wyślij wiadomość

    Strona główna