Senin, 13 Mei 2013

Exception Pada Java


Exception Pada Java


Assalammualaikum.. siang ini gue akan bahas program java tentang Exception handling.
Pertama dijava itu ada banyak jenis exception yang berada dipaket java.lang.object
Error: pada bagian ini bersifat fatal, kenapa? Karena tidak bisa dimanipulasi untuk diperbaiki.
KelasNya : LinkageError, AWTError dan VirtualMachineError.
Exception : Jenis ini tentu dapat diantisipasi jika terdapat statement berpotensi yang dapat menimbulkan error.
Pada bagian exception juga ada beberapa bagian yang diperiksa, ada yang langsung ditangani dan ada juga yang di lempar (throw). Dan ada juga yang tidak diperiksa oleh interpreter. Tetapi programmer juga dapat membuat exception sendiri dengan beberapa cara seperti dengan menggunakan extends, implements dari kelas exception .
Tabel yang mengecek Exception


Cara mendeklarasikannya:
Try{

// pemanggilan metode yang akan menghasilkan exception

//blok statement

}

Catch(TipeException1 nameObject){

//penanganan exception

}

Catch(TipeException2 nameObject){
//penanganan exception

}
Catch(TipeException3 nameObject){

//penanganan exception
}

Finally{

//blok ini akan selalu di ekseskusi
}

Contoh Program
Program 1:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication33;  
import java.util.*; 
/**
 *
 * @author Aspire
 */
public class JavaApplication33 {
static Scanner input = new Scanner(System.in);
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int angka; 
        try{
            System.out.print("Masukkan Angka = ");
            angka = input.nextInt();
        }catch(Exception e)
        {
            System.out.println("Input Pake Angka . . bukan Huruf . . .");
        }
        System.out.println("\nPress any key to continue . . . ");
    }
}

Program 2:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication35;
import java.util.Scanner;  
import java.util.InputMismatchException;
/**
 *
 * @author Aspire
 */
public class JavaApplication35 {
public static int divide(int numerator, int denumerator) throws ArithmeticException {  
        return numerator / denumerator;  
    }  
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);  
        boolean lanjut = true;  
  
        do {  
            try {  
                System.out.println("Masukan Numerator: ");  
                int numerator = in.nextInt();  
                System.out.println("Masukan Denumerator: ");  
                int denumerator = in.nextInt();  
                System.out.println("Hasil Pembagiannya Adalah : " + divide(numerator, denumerator));  
                lanjut = false;  
            } catch (InputMismatchException inputMismatchException) {  
                System.err.println("Exception : " + inputMismatchException);  
                in.nextLine();  
                System.out.println("Masukanlah Integer!!!");  
                System.out.println("Cobalah Lagi!!");  
            } catch (ArithmeticException arithmeticException) {  
                System.err.println("Exception : " + arithmeticException);  
                System.out.println("Mencoba Membagi Bilangan Dengan 0 ");  
                System.out.println("Cobalah Lagi!!");  
            }  
        } while (lanjut);  
    }
}

Program 3:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication38;
import java.util.*;
 
/**
 *
 * @author Aspire
 */
public class JavaApplication38 {
public static Scanner input = new Scanner(System.in);
public static String Login1()
{
String pengguna;
System.out.print("Masukkan Username : ");
pengguna=input.next();
return pengguna;
}
public static String Login2()
{
String id;
System.out.print("Masukkan Id       : ");
id=input.next();
return id;
}
private static void konfigurasi(String user, String pasword)
{
boolean error;error = true;
while (error==true)
{
try
{
while(!user.equalsIgnoreCase("Bella") || !pasword.equals("12345"))
{
System.out.println();
System.out.println(" **Login Failed **");
System.out.println();
user = Login1();
pasword = Login2();
}
error = false;
}
catch(Exception e)
{
System.out.println();
System.out.println("# User & Pasword Salah");
System.out.println();
}
}
}
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String user,pasword;
user = Login1();
pasword = Login2();
konfigurasi(user,pasword);
System.out.println();
System.out.println(" **SELAMAT Login Sukses** ");
System.out.println();
 
System.out.print("Press any key to continue . . . ");
System.out.println(true);
    }
}

Silakan coba sendiri hasilnya :)
Sekian moga bermanfaat




0 komentar:

Posting Komentar

© Bella Yulianita 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis