– 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

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) {
 System.out.println("tidak ada exception yang di tangkap, masuk finallly");


Create Exception

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){

public ExceptionPertama(OwnError ownError) {

public ExceptionPertama(OwnErrorSingle ownErrorSingle) {

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.

package IFM.utils.constants;

public enum OwnError {

private String symbol;
 private String name;

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

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


package IFM.utils.constants;

package IFM.utils.constants;

public enum OwnErrorSingle {


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


    April 25, 2013 at 7:50 am

Leave a Reply

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

You are commenting using your 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: