Selasa, 14 Agustus 2012
Demo Throws
class Barang {
private String kode;
private String nama;
private double harga;
public void setKode(String vKode) {
try {
kode = vKode;
if (kode == null) {
throw new NullPointerException();
}
} catch (NullPointerException npe) {
System.out.println("KESALAHAN: " +
"Kode barang tidak boleh null");
}
}
public String getKode() {
return kode;
}
public void setNama(String vNama) {
try {
nama = vNama;
if (nama == null) {
throw new NullPointerException();
}
} catch (NullPointerException npe) {
System.out.println("KESALAHAN: " +
"Nama barang tidak boleh null");
}
}
public String getNama() {
return nama;
}
public void setHarga(int vHarga) {
harga = vHarga;
}
public double getHarga() {
return harga;
}
}
class DemoThrow {
public static void main(String[] args) {
Barang obj = new Barang();
obj.setKode(null);
obj.setNama("Buku tulis");
obj.setHarga(2500);
System.out.println("\nKode : " + obj.getKode());
System.out.println("Nama : " + obj.getNama());
System.out.println("Harga : " + obj.getHarga());
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar