Introduction:
To develop the project, we must create our own Exception
class, as Sun microsystem has not given any exception class some requirements
which are not in common.
So, what is User defined exception is?
Exception class defined by Developer is called as User Defined
Exception or Custom exception
Steps to create User Defined Exception:
- Write a class that extends java.lang.Exception.
- In User defined class, define minimum 2 constructors.
- No-argument constructor
- String parameter with logic “super(message)” to create exception object with message.
- No need to define any method, why because exception class is not meant for developing business logic , instead it is used for terminating method logic execution by throwing it’s object.
------------------------------------------------------------------------------------------------------------
package
com.app.exceptions;
/**
*
* @author Shashank Mungantiwar
*
*/
public class NegativeNumberException extends Exception {
private static final long serialVersionUID = 1L;
public NegativeNumberException() {
super();
}
public NegativeNumberException(String message) {
super(message);
}
}
/**
*
* @author Shashank Mungantiwar
*
*/
public class NegativeNumberException extends Exception {
private static final long serialVersionUID = 1L;
public NegativeNumberException() {
super();
}
public NegativeNumberException(String message) {
super(message);
}
}
----------------------------------------------------------------------------------------------------------
Why should we write super(message) statement in String parameter constructor, what is the internal problem?
- To pass the given message to Throwable class. To prepare it’s Exception object with given message.
- If you don’t write super(message), message is not handover to Throwable class and given message is not printed.
Why should we write User defined Exception or Custom Exception class subclassing from Exception class?
- We are writing custom exception to represent a condition failure logical mistake.
- So, when this exception is throw it must be report. Hence it should be subclass of Exception to be validated by compiler.
- We should derive or extend it from RuntimeException class only if we don’t want it validated by compiler or if we are writing it for representing operator failure.
- We should extend it from Error class when we want to represent logical mistake occurred in JVM logic.
- We should extend it from Throwable to create separate category. It is not at all recommended.
- So, according to above point I explained, User defined class should be subclass of Exception class.
Now we
will implement our custom exception-
----------------------------------------------------------------------------------------------------------
package
com.app.bussinesslogic;
/**
*
* @author Shashank Mungantiwar
*
*/
import com.app.exceptions.NegativeNumberException;
public class Addition {
public static int add(int a, int b) throws NegativeNumberException {
if (a < 0) {
throw new NegativeNumberException("Input first number=" + a + " should be non-negative");
}
if (b < 0) {
throw new NegativeNumberException("Input Second number=" + b + " should be non-negative");
}
return a + b;
}
}
/**
*
* @author Shashank Mungantiwar
*
*/
import com.app.exceptions.NegativeNumberException;
public class Addition {
public static int add(int a, int b) throws NegativeNumberException {
if (a < 0) {
throw new NegativeNumberException("Input first number=" + a + " should be non-negative");
}
if (b < 0) {
throw new NegativeNumberException("Input Second number=" + b + " should be non-negative");
}
return a + b;
}
}
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
package
com.app.user;
/**
*
* @author Shashank Mungantiwar
*
*/
import java.util.Scanner;
import com.app.bussinesslogic.Addition;
import com.app.exceptions.NegativeNumberException;
public class Calculator {
public static void main(String[] args) throws NegativeNumberException{
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Number:");
int firstnumber = scanner.nextInt();
System.out.println("Enter Second Number:");
int secondnumber = scanner.nextInt();
int result = Addition.add(firstnumber, secondnumber);
System.out.println("Result=" + result);
}
}
/**
*
* @author Shashank Mungantiwar
*
*/
import java.util.Scanner;
import com.app.bussinesslogic.Addition;
import com.app.exceptions.NegativeNumberException;
public class Calculator {
public static void main(String[] args) throws NegativeNumberException{
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Number:");
int firstnumber = scanner.nextInt();
System.out.println("Enter Second Number:");
int secondnumber = scanner.nextInt();
int result = Addition.add(firstnumber, secondnumber);
System.out.println("Result=" + result);
}
}
----------------------------------------------------------------------------------------------------------
I will
not write the output of above project , when input is given as negative number,
you write the code and execute it.
As per
output , the Java exception message is not understandable by the User.
So, it’s
developer’s responsibility to convert java exception object message to
end user understandable message relevant to current logical mistake.
How is it possible?
Yes. You
guess it correct using try-catch block
- try establish a block to write exception causing statements and it’s related statements also we can write normal statements.
- catch block is parameterised block used for catching the exceptions raised in it’s corresponding try block, and to define the logic to take necessary actions against that caught exception.
- Necessary actions include -
- Printing user understandable message relevant this exception.
- To stop abnormal terminations.
- To execute some other useful logic.
- Rewrite Calculator program to print user understandable message, instead of java exception message.
- In above we got 2 exceptions-
- InputMisMatchException thrown by nextInt() from Scanner class, if given input is not int type.
- NegativeNumberException thrown by add() method if given input is negative.
- So, we must write 2 catch blocks to catch 2 exceptions separately to print user understandable message specific to exception as shown in below code snippet:
----------------------------------------------------------------------------------------------------------
package com.app.user;
/**
*
* @author Shashank Mungantiwar
*
*/
import java.util.InputMismatchException;
import java.util.Scanner;
import com.app.bussinesslogic.Addition;
import com.app.exceptions.NegativeNumberException;
public class Calculator {
public static void main(String[] args) {
try {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Number:");
int firstnumber = scanner.nextInt();
System.out.println("Enter Second Number:");
int secondnumber = scanner.nextInt();
int result = Addition.add(firstnumber, secondnumber);
System.out.println("Result=" + result);
} catch (InputMismatchException inputMismatchException) {
System.out.println("Enter only Integer value...!!!");
} catch (NegativeNumberException negativeNumberException) {
System.out.println("Please " + negativeNumberException.getMessage());
}
}
}
/**
*
* @author Shashank Mungantiwar
*
*/
import java.util.InputMismatchException;
import java.util.Scanner;
import com.app.bussinesslogic.Addition;
import com.app.exceptions.NegativeNumberException;
public class Calculator {
public static void main(String[] args) {
try {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Number:");
int firstnumber = scanner.nextInt();
System.out.println("Enter Second Number:");
int secondnumber = scanner.nextInt();
int result = Addition.add(firstnumber, secondnumber);
System.out.println("Result=" + result);
} catch (InputMismatchException inputMismatchException) {
System.out.println("Enter only Integer value...!!!");
} catch (NegativeNumberException negativeNumberException) {
System.out.println("Please " + negativeNumberException.getMessage());
}
}
}
----------------------------------------------------------------------------------------------------------
Thank You
...!!!
Comments
Post a Comment