Throw
Keyword Ini Digunakan Untuk Melemparkan Suatu Bug Atau Kesalahhan Yang Dibuat Setutor Manual. Dididalem Java inilah Contoh ProgramnyaContoh Program Throw 1
public class Throw { public static void main(String[] args) { try { throw new Exception ("Kesalahan Terjadi"); }catch (Exception e){ System.out.println(e); } } }
Contoh Program Throw 2
public class Throw2 { static void demoproc(){ try{ throw new NullPointerException ("demo"); }catch(NullPointerException e){ System.out.println("Caught Inside demoproc"); throw e; } } public static void main(String[] args) { try { demoproc(); }catch (NullPointerException e){ System.out.println("Recaught:"+e); } } }
Contoh Program Throw 3
public class Throw3 { public static void methodlain() { try{ throw new ArrayIndexOutOfBoundsException (1); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Penanganan Eksepsi didalem method method lain"); throw e; } } public static void main(String[] args) { try{ methodlain(); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Penanganan Eksepsi Dalam Method Main"); } } }
Contoh Program Throw 4
public class Throw4 { public static void main(String[] args) { try { throw new Exception ("Kesalahan Terjadi"); }catch(Exception e){ System.out.println(e); } } }
Throws
Keyword throws dipakai didalem suatu method ataupun kelas nan sanggup saja menghasilkan suatu kesalahan risikonya perlu ditangkap erornya..Contoh Program Throws 1
public class Throws { void method1() throws ArithmeticException { throw new ArithmeticException("Calculate Error"); } void method2()throws ArithmeticException { method1(); } void method3(){ try { method2(); } catch(ArithmeticException e) { System.out.println("Arithmetic Exception Handled"); } } public static void main(String[] args) { Throws obj = new Throws(); obj.method3(); System.out.println("End Of Program"); } }
Contoh Program Throws 2
public class Throws2 { void method() throws ArithmeticException { throw new ArithmeticException ("Arithmetic Exception Occured"); } } class Example1 { public static void main(String[] args) { Throws2 obj = new Throws2(); obj.method(); System.out.println("End Of Program"); } }
Contoh Program Throws 3
public class Throws3 { public static void main(String[] args) { try { f(); }catch (Exception e){ System.out.println(e); } } public static void f() throws NullPointerException,ArrayIndexOutOfBoundsException { throw new NullPointerException(); } }
Contoh Program Throws 4
public class Throws4 { public static void main(String[] args) { try { f(); }catch (Exception b){ System.out.println(b); } } public static void f() throws NullPointerException,ArrayIndexOutOfBoundsException { throw new ArrayIndexOutOfBoundsException(); } }
Penjelasan...
Throw
Keyword Throwitu dipakai sebagai melempar suatu kesalahan ataupun exception dididalem jadwal itu, Sebagai Contoh :If (ada nan tidak benar)Throw new Exception("Terdapat Kesalahan Disini");
Throws
Keyword Throws dipakai di waktu mendeklarasikan mendeklarasikan suatu method sebagai memberi tahu bahwa method nan bersangkutan sanggup melepar eksepsi dengan tipe nan dideklarasikan oleh keyword throws itu, misalnya begini :Void methodlain()throwsIOException,Exception{}
Penutup..
Baiklah hingga disitu kalian boleh mencobanya di komputer kalian masing masing, saatitu java memang rampung belomasing lagi sebagai kalangan orang, bahkan dunia rampung memakai ini, demikianlah artikelitu nan berjudul Contoh Program Throw Dan Throws Pada Java supaya berfungsi juga sanggup kalian terapkan dirumah masing masing :)
Tidak ada komentar:
Posting Komentar