0% found this document useful (0 votes)
83 views

Hands On - Exception Handling

Date validation with try-catch, price validation with a user-defined exception, and number division with finally are demonstrated across

Uploaded by

Vamsi Gembali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Hands On - Exception Handling

Date validation with try-catch, price validation with a user-defined exception, and number division with finally are demonstrated across

Uploaded by

Vamsi Gembali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Validate Date using ParseException - Use try catch throws

import java.util.Scanner;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);

//FILL THE CODE


String str = sc.nextLine();
try{
Date parsedDate = convertStringToDate(str);
System.out.println(str+" is a valid date");
}
catch(ParseException e){
System.out.println(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();
df.setLenient(false);
date = df.parse(str);
return date;
}

-----------------------------------------------------------------------------------
-----------------------------
Display Product Details - User defined Exception

import java.util.Scanner;

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(System.in);
System.out.println("Enter product name");
String str = sc.nextLine();
System.out.println("Enter price");
try{
int n =sc.nextInt();
validate(n);
System.out.println("Name : "+str);
System.out.println("Price : "+n);
}

catch(InvalidPriceException e){
System.out.println(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 java.util.*;

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 = str.concat(". Thanks for using the application.");
return str;
}

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

Scanner sc = new Scanner(System.in);


Division obj = new Division();
System.out.println("Enter the numbers");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(obj.divideTwoNumbers(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) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getExpectedSalary() {
return expectedSalary;
}
public void setExpectedSalary(double expectedSalary) {
this.expectedSalary = expectedSalary;
}
}

import java.util.*;

public class Main{

public static Candidate getCandidateDetails() throws InvalidSalaryException{

Scanner sc = new Scanner(System.in);


System.out.println("Enter the candidate Details");
Candidate cand = new Candidate();
System.out.println("Name");
String name = sc.nextLine();
cand.setName(name);
System.out.println("Gender");
String gender = sc.nextLine();
cand.setGender(gender);
System.out.println("Expected Salary");
double sal =sc.nextDouble();
if(sal<10000){
throw new InvalidSalaryException("Registration Failed. Salary cannot be
less than 10000.");

}
else{
cand.setExpectedSalary(sal);
return cand;
}
}
public static void main (String[] args) {

Main exp = new Main();


Candidate exp2;
try{
exp2=exp.getCandidateDetails();
System.out.println("Registration Successful");
}
catch(InvalidSalaryException e){
System.out.println(e.getMessage());
}
}
}

public class InvalidSalaryException extends Exception{


public InvalidSalaryException (String s){
super(s);
}
}

-----------------------------------------------------------------------------------
------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy