snippetjournal

– notes, snippet codes, and my journal

Create Your Own Exception and Enum

with 2 comments

Terkadang dalam suatu project kita diharuskan membuat exception yang di custom agar handling error mudah di pahami.
Meng-custom exception pun mudah,dengan meng-extends class exception pada java. berikut contoh nya:

main java

ExceptionMainTest.java


package IFM.ActionController;

import IFM.utils.exceptions.ExceptionPertama;
import IFM.utils.constants.OwnError;
import IFM.utils.constants.OwnErrorSingle;

public class ExceptionMainTest {

public static void main(String[] args) {
 try {

System.out.println("test uji coba exception dengan enum");

String buatError = "asatu";

if (OwnErrorSingle.ERROR_SATU.equals("satu")) {
 throw new ExceptionPertama(OwnErrorSingle.ERROR_SATU);
 }else if(OwnErrorSingle.ERROR_SATU.equals("dua")) {
 throw new ExceptionPertama(OwnErrorSingle.ERROR_DUA);
 }

if (OwnError.ERROR_LAGI_SATU.getSymbol().equals(buatError)) {
 throw new ExceptionPertama(OwnError.ERROR_LAGI_SATU.getSymbol());
 } else if (OwnError.ERROR_LAGI_SATU.getSymbol().equals(buatError)) {
 throw new ExceptionPertama(OwnError.ERROR_LAGI_SATU.getName());
 }

/*else {
 throw new ExceptionPertama(OwnError.ERROR_LAGI_SATU);
 }*/

} catch (ExceptionPertama ep) {
 ep.printStackTrace();
 }finally{
 System.out.println("tidak ada exception yang di tangkap, masuk finallly");
 }

}
}

Create Exception
ExceptionPertama.java


package IFM.utils.exceptions;

import IFM.utils.constants.OwnError;
import IFM.utils.constants.OwnErrorSingle;

public class ExceptionPertama extends Exception {
 public ExceptionPertama() {
 }

 public ExceptionPertama(String pesan){
 super(pesan);
 }

public ExceptionPertama(OwnError ownError) {
 super(ownError.toString());
 }

public ExceptionPertama(OwnErrorSingle ownErrorSingle) {
 super(ownErrorSingle.toString());
 }
}

Constant using enum

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type’s fields are in uppercase letters.

In the Java programming language, you define an enum type by using the enum keyword.

OwnError.java


package IFM.utils.constants;

public enum OwnError {
 ERROR_LAGI_SATU("symbol","name"),
 ERROR_LAGI_DUA("2";,"dua"),
 ERROR_LAGI_TIGA("3","tiga");

private String symbol;
 private String name;

public String getSymbol() {
 return symbol;
 }
 public String getName() {
 return name;
 }

OwnError(String symbol, String name){
 this.symbol = symbol;
 this.name = name;
 }
}

OwnErrorSingle,java


package IFM.utils.constants;

package IFM.utils.constants;

public enum OwnErrorSingle {
 ERROR_SATU, ERROR_DUA, ERROR_TIGA, ERROR_EMPAT, ERROR_LIMA
}

Written by snippetjournal

November 25, 2012 at 1:21 pm

Posted in Programming

Tagged with , , ,

2 Responses

Subscribe to comments with RSS.

  1. nah ini dia….. enum

    ikhsan

    April 25, 2013 at 7:50 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: