Validate Date using ParseException - Use try catch throws
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
//FILL THE CODE
String str = [Link]();
try{
Date parsedDate = convertStringToDate(str);
[Link](str+" is a valid date");
}
catch(ParseException e){
[Link](str+" is not a valid date");
}
public static Date convertStringToDate(String str) throws ParseException {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
[Link](false);
date = [Link](str);
return date;
}
-----------------------------------------------------------------------------------
-----------------------------
Display Product Details - User defined Exception
import [Link];
public class Main {
static void validate(int price) throws InvalidPriceException{
if (price<100)
throw new InvalidPriceException("InvalidPriceException");
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter product name");
String str = [Link]();
[Link]("Enter price");
try{
int n =[Link]();
validate(n);
[Link]("Name : "+str);
[Link]("Price : "+n);
}
catch(InvalidPriceException e){
[Link](e+" - Product price invalid");
}
//FILL THE CODE
}
}
//This class should inherit Exception class
public class InvalidPriceException extends Exception
{
//Provide a single argument constructor
public InvalidPriceException(String s){
-----------------------------------------------------------------------------------
-----------
Divide two numbers - Use finally
import [Link].*;
public class Division{
public String divideTwoNumbers(int number1,int number2){
String str = "";
int res;
try {
res = number1/number2;
str = "The answer is "+res;
} catch(ArithmeticException e) {
str = "Division by zero is not possible";
} finally {
str = [Link](". Thanks for using the application.");
return str;
}
}
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
Division obj = new Division();
[Link]("Enter the numbers");
int a = [Link]();
int b = [Link]();
[Link]([Link](a,b));
}
}
-----------------------------------------------------------------------------------
----------
Register a Candidate - User defined Exception(with throw and throws)
public class Candidate {
private String name;
private String gender;
private double expectedSalary;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
[Link] = gender;
}
public double getExpectedSalary() {
return expectedSalary;
}
public void setExpectedSalary(double expectedSalary) {
[Link] = expectedSalary;
}
}
import [Link].*;
public class Main{
public static Candidate getCandidateDetails() throws InvalidSalaryException{
Scanner sc = new Scanner([Link]);
[Link]("Enter the candidate Details");
Candidate cand = new Candidate();
[Link]("Name");
String name = [Link]();
[Link](name);
[Link]("Gender");
String gender = [Link]();
[Link](gender);
[Link]("Expected Salary");
double sal =[Link]();
if(sal<10000){
throw new InvalidSalaryException("Registration Failed. Salary cannot be
less than 10000.");
}
else{
[Link](sal);
return cand;
}
}
public static void main (String[] args) {
Main exp = new Main();
Candidate exp2;
try{
exp2=[Link]();
[Link]("Registration Successful");
}
catch(InvalidSalaryException e){
[Link]([Link]());
}
}
}
public class InvalidSalaryException extends Exception{
public InvalidSalaryException (String s){
super(s);
}
}
-----------------------------------------------------------------------------------
------------------------