Custom Exception in Java

Custom Exception in Java

Custom Exception in Java

 

Custom Exception in Java .Means building self customized Exception classes to handle exception Error occur in your program .

About The Sample Code

In this Sample Code i will build 5 Classes to help you to understand what is Customized Exception Handling

  1. warCharacter
  2. armors
  3. weapons
  4. createTroop
  5. troopException

 

First I will Start with Troop Exception

Troop Exception

 


package customException;

public class troopException extends  Exception {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -357858946558175626L;

	public troopException (String message){
		
		super(message);
		
		
	}

}



  1. First create a child Class from the Build in Exception Class
  2. You can assign your own Exception Serial Number or get it auto generated
  3. Create a Constructor
  4. Call the Parent Class (“Exception”) in the troop Exception Child Class
  5. Exception is the Super Class for troopException

 

warCharacter

package customException;

public class warCharacter<T,A> {
	
	// Types of Input
	
		T weapon;
		A armor;
		
		private int troopType;
		
		
		// THis is a Constructor
		
		public warCharacter(T weapon, A armor ,int troopType)throws troopException {
			
			  this.setTroopType(troopType);
		
		if( troopType== 1||troopType== 2||troopType== 3){
		
			  // super();
		       this.weapon = weapon;
			   this.armor = armor;
			
		}
		
		else{
		
		    throw new troopException (" You Have Selected the Wrong Troop Type");
			
			
			}
		}

		
		
		// Get Info ( armor)
		
		public A getArmor() {
			return armor;
		}

	   // Set Info ( armor)

		public void setArmor(A armor) {
			this.armor = armor;
		}




		// Get Info ( armor)
		
		
		 public T getWeapon() {
			return weapon;
		}
		 
		// Set Info ( armor)

		public void setWeapon(T weapon) {
			this.weapon = weapon;
		}



		public int getTroopType() {
			return troopType;
		}



		public void setTroopType(int troopType) {
			this.troopType = troopType;
		}


}



  1. First create the Generic Type War Character Class
  2. Then I Declare the warCharacter Constructor which will throw the “troopException ” Exception in the event an Error occur
  3. If the User input any value beside “1/2/3” into the troopType variable , the program will throw an Exception Message
  4. There are different between throws and throw
  5. Throws -> Handle at the Class Level
  6. Throw -> Handle inside the Class . A class might contains many sub Exception
  7. Finally i Auto Generate all the getter and setter

 

weapons

Then i create the Weapon Class

package customException;

public class weapons {
	
	
	
	public String weaponName;
	
	public weapons(String weaponName){
	
	                super();
	                this.weaponName = weaponName;
	
	}
	
	//Get Info

		public String getWeaponName() {
			return weaponName;
		}
		
		// Set Info

		public void setWeaponName(String weaponName) {
			this.weaponName = weaponName;
		}
	
	
	

}

 

armors

package customException;

public class armors {
	
public String armorName;
	
	// This is a constructor
	public armors(String armorName) {
		super();
		this.armorName = armorName;
	}
	
	// Get Info
	
	public String getArmorName() {
		return armorName;
	}
	
	// Set Info

	public void setArmorName(String armorName) {
		this.armorName = armorName;
	}
	
	

}


 

createTroop


package customException;

import java.util.*;



public class createTroop {
	

	static int soldierTypes;
	static String weaponTypes;
        static String armorTypes;

	
	
	

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

		Scanner inputScan1 = new Scanner(System.in);
		Scanner inputScan2 = new Scanner(System.in);
		Scanner inputScan3 = new Scanner(System.in);
		
		
		System.out.println("Enter the Type of Soldier you wnat to create ?< 1 = Melee , 2 = Archer , 3 = Wizzard> ");
		
		soldierTypes = inputScan1.nextInt();
	
	    System.out.println("Name the New Weapon for your Troops ");
	
		weaponTypes = inputScan2.nextLine();
	
	    System.out.println("Name the New Armor for your Troops ");
		armorTypes = inputScan3.nextLine();
		
		
		
		weapons sacredSword = new weapons(weaponTypes);
		
		armors noArmor = new armors(armorTypes);
		
		warCharacter<weapons,armors> newtroop = new warCharacter<weapons,armors>(sacredSword,noArmor,soldierTypes);
		
		
	    
	    String newWeapon  = newtroop.weapon.getWeaponName();
	    
	    String newArmor = newtroop.armor.getArmorName();

		System.out.println("Your new Troop  weapon  named "+newWeapon);
		
		System.out.println("Your new Troop  "+newArmor);

		
		}
		
		
		
		
		catch (troopException e){
			System.out.println(e.getMessage());
			
			
			
		}
		catch(Exception e)
		{
			
			System.out.println("Wrong Input try again !");
			
		       System.out.println("You must enter an integer");	
			
			
		}
		
		
	}

}



  1. First , Import Java Util to enable you to use Scanner Input
  2. Declare all the Variable that will be transfered from the Input Scanner
  3. Use the Try Catch Block
  4. Print out a Statement to ask the User to select type of Soldier wanted to be created
  5. Print out a Statement to ask the User what weapon or armor to be assign to the new Troop to be created
  6. Throw -> Instantiate the ” newTroop” Instant , by assigning the inputed Weapon and armor
  7. Finally print out the Weapon and Armor of the new troop being created
  8. (1) Catcth -> The first Catch Exception will check whether the User enter the Correct Soldier type
  9. (2) Catcth -> The Seconf Catch Exception will check whether the user enter the correct Type(” Which is integer”) in this Program

 

Well thats the End for my Custom Exception Blog, hopefully that help you out

Leave a Reply

Your email address will not be published. Required fields are marked *

20 − 11 =