Thursday 11 July 2013

Creating Custom exception in java

1. Define a class and extend Throwable or Exception class


package tts.test.exception;
public class InvalidAmountException extends Throwable {
private static final long serialVersionUID = 1L;
public InvalidAmountException(String message) {
super(message);
}
}


2. To test user defined exception create a main class and use throw your own defined exception inside try block and can be handled inside catch another way is to handle inside header of main method here example showing handling inside header of main method
NOTE: User defined exception are mostly checked exception unless you will not categorized any run time exception

package tts.test.exception;

import java.util.Scanner;
public class TestCustomException {
public static void main(String[] args) throws InvalidAmountException {
double acc_balance = 50_000;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your amount to withdraw:");
double withdraw_amount = scanner.nextDouble();
if (acc_balance < withdraw_amount) {
throw new InvalidAmountException(
"Sorry! Your amount is invalid or exceding limit.");
}
System.out.println("Thank for using Personal Bank!!!");
}
}


3 Out put 

If entered amount is less then 50000

Enter your amount to withdraw:
1000
Thank for using Personal Bank!!!
-------------------------------------------------------

If amount is greater than 50000

Enter your amount to withdraw:
600000
Exception in thread "main" tts.test.exception.InvalidAmountException: Sorry! Your amount is invalid or exceding limit.
at tts.test.exception.TestCustomException.main(TestCustomException.java:11)


3. Now lets  dig more into java exceptions from  the above class extending InputMismatchException and see there is no need of adding throws or try catch at compile or declaration time means exception will be fire at run time only if any condition violates. Lets see  what is happening here at run time




package tts.test.exception;
import java.util.InputMismatchException;
public class InvalidAmountException extends InputMismatchException {
private static final long serialVersionUID = 1L;
public InvalidAmountException(String message) {
super(message);
}
}


Now  test the new custom exception class 



package tts.test.exception;
import java.util.Scanner;
public class TestnputMismatchException {
public static void main(String[] args) {
double acc_balance = 50_000;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your amount to withdraw:");
double withdraw_amount = scanner.nextDouble();
if (acc_balance < withdraw_amount) {
throw new InvalidAmountException(
"Sorry! Your amount is invalid or exceding limit.");
}
System.out.println("Thank for using Personal Bank!!!");
}
}


4. Out put


When input is: 

Enter your amount to withdraw:
1000
Thank for using Personal Bank!!!
----------------------------------------
When input contain ant illegal  number as any character

Enter your amount to withdraw:
111a12
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)

at tts.test.exception.TestnputMismatchException.main(TestnputMismatchException.java:10)



Hope it will be usefull

Thank You



No comments:

Post a Comment