Exception Handling

Exception handling adalah penangan error yang terjadi pada sebuah program. Exception handling akan mendeteksi error yang terjadi secara otomatis. Exception handling dalam pemrograman Csharp, Java dan C++ menggunakan keyword “try-cactch”.


namespace Exception_Handling
 {
     class Program
     {
         private static string Tempat()
         {
             string status;
             Console.WriteLine("Apakah STIKOM Banyuwangi berada di Banyuwangi ?");
             Console.Write("Jawaban (Ya / Tidak) : ");
             status = Convert.ToString(Console.ReadLine());
             return status;
         }
         <b>private</b> static string <b>Jurusan</b>()
         {
             string jur;
             Console.<b>WriteLine</b>("\nBerapa jumlah Program Studi yang ada di STIKOM Banyuwangi ? ");
             Console.<b>Write</b>("Jawaban : ");
             jur = Convert.<b>ToString</b>(Console.<b>ReadLine</b>());
             return jur;
         }
         <b>private</b> static <b>int</b> <b>Penjumlahan</b>()
         {
             <b>int</b> hasil;
             Console.<b>WriteLine</b>("\nSoal 1+2+3+4+5+6+7+8+9");
             Console.<b>Write</b>("Input Hasil Penjumlahan : \n");
             hasil = Convert.<b>ToInt32</b>(Console.<b>ReadLine</b>());
             return hasil;
         }
         <b>private</b> static void <b>Tanya_1</b>(string ask1)
         {
             <b>bool</b> cek1 = <b>true</b>;
             <b>while</b> (cek1 == <b>true</b>)
             {
                 <b>try</b>
                 {
                     <b>while</b>(ask1!="Ya")
                     {
                         Console.<b>WriteLine</b>("Jawaban Salah \n",ask1);
                         ask1 = <b>Tempat</b>();
                     }
                     cek1 = <b>false</b>;
                 }
                 <b>catch</b>(Exception)
                 {
                     Console.<b>WriteLine</b>("Jawaban Salah \n",ask1);
                 }
             }
         }
         <b>private</b> static void <b>Tanya_2</b>(string ask2)
         {
             <b>bool</b> cek2 = <b>true</b>;
             <b>while</b> (cek2 == <b>true</b>)
             {
                 <b>try</b>
                 {
                     <b>while</b>(ask2!="2")
                     {
                         Console.<b>WriteLine</b>("Jawaban Salah \n",ask2);
                         ask2 = <b>Jurusan</b>();
                     }
                     cek2 = <b>false</b>;
                 }
                 <b>catch</b>(Exception)
                 {
                         Console.<b>WriteLine</b>("Jawaban Salah\n",ask2);
                 }
             }
         }
         <b>private</b> static void <b>TambahTambahan</b>(<b>int</b> penjumlahan)
         {
             <b>bool</b> cek3 = <b>true</b>;
             <b>while</b> (cek3 == <b>true</b>)
             {
                 <b>try</b>
                 {
                     <b>while</b>(penjumlahan!=45)
                     {
                         Console.<b>WriteLine</b>("Jawaban Salah = {0}\n",penjumlahan);
                         penjumlahan = <b>Penjumlahan</b>();
                     }
                     cek3 = <b>false</b>;
                 }
                 <b>catch</b>(Exception)
                 {
                     Console.<b>WriteLine</b>("Jawaban Salah = {0}\n",penjumlahan);
                 }
             }
         }
         <b>public</b> static void <b>Main</b>(string[]args)
         {
             string tempat;
             string jurusan;
             <b>int</b> itung;

             tempat = <b>Tempat</b>();
             <b>Tanya_1</b>(tempat);
             jurusan = <b>Jurusan</b>();
             <b>Tanya_2</b>(jurusan);
             Console.<b>WriteLine</b>("Setelah menjawab pertanyaan di atas, mari tes kemampuan berhitung anda :D\n");
             itung = <b>Penjumlahan</b>();
             <b>TambahTambahan</b>(itung);

             Console.<b>WriteLine</b>("Selamat, jawaban anda BENAR semua");
             Console.<b>ReadKey</b>(<b>true</b>);
         }

     }

 }

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s