More exceptions (j4j)
Da Sinapsi.
package com.sinapsi.j4j;
public class MoreExceptions {
private int _instanceIndex;
private Object _instanceObject;
public MoreExceptions(int instanceIndex) {
_instanceIndex= instanceIndex;
if (instanceIndex == 2) {
_instanceObject = new Long(instanceIndex);
} else if (instanceIndex %2 ==0) {
_instanceObject = "instance";
}
}
private void doStuff() {
String str = (String)_instanceObject;
int length = str.length()/_instanceIndex;
}
/**
* SENZA ESEGUIRE QUESTO CODICE, SCRIVI SU UN FOGLIO COSA TI ASPETTI CHE FACCIA.
* POI ESEGUI IL CODICE E CONFRONTA IL COMPORTAMENTO CON LE TUE PREVISIONI.
* PERCHE' SI COMPORTA COSI'?
*/
public static void main(String[] args) {
MoreExceptions[] instances = new MoreExceptions[] {
new MoreExceptions(0),
new MoreExceptions(1),
new MoreExceptions(2)
};
for (int i=0; i<4; i++) {
try {
System.out.println(i);
instances[i].doStuff();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
}
[edit]
Il baco pernicioso (per i solutori più che abili)
Aggiungere questa chiamata nel main.
System.out.println(instances[0]);
E questo metodo alla classe.
public String toString() {
return "questo oggetto e' " + this;
}
