Exception Handling
Exception Handling
/*Exception Handling
* */
int a=input.nextInt();
int b=input.nextInt();
//program abnormally
System.out.println("Success");
int a = input.nextInt();
int b = input.nextInt();
/*
*Exception Handler
*1. enclose the statements with in try which will monitor the
*code if any error occur it creates Object and throws
*/
try {
System.out.println(a / b);
}catch(ArithmeticException e) {
b=input.nextInt();
System.out.println(a/b);
System.out.println("Success");
/*
* Unchecked Exception
* 1.Arithmetic
* 2.indexouofbounds
* 3.InputMismatch
* 4.NumberFormat
* 5.Null pointer
*/
//int a=Integer.parseInt(input.nextLine());//NumberFormat
a=input.nextInt();//Inputmismatch
*******************************************************************
a=input.nextInt();//Inputmismatch
/*
* Unchecked Exception
* 1.Arithmetic
* 2.indexouofbounds
* 3.InputMismatch
* 4.NumberFormat
* 5.Null pointer
* Checked exception
* 1.parse exception
* 2.SqlException
* 3.FilenotFound
*/
1.try{
* }catch(){
*}
* 2.try{
* }finally{
*}
* 3.try{
* }catch{
* }finally{
*}
int a[];
int n;
try {
n = input.nextInt();
a = new int[n];
a[i] = input.nextInt();
System.out.println("Invalid input");
int a[];
int n;
try {
n = input.nextInt();
if(n<0)
throw new Exception();
} catch(Exception e) {
System.out.println(e);
package sample;
//uncheked ex RuntimeException
InvalidAge(String msg){
super(msg);
package sample;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
int age=12;
if(age>=25) {
package sample;
//uncheked ex RuntimeException
InvalidAge(String msg){
super(msg);
package sample;
super();
this.countryCode = countryCode;
this.countryName = countryName;
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
return countryName;
this.countryName = countryName;
@Override
package sample;
import java.util.ArrayList;
import java.util.List;
if(country.getCountryName().matches("[a-zA-Z ]{1,}"))
countryList.add(country);
else
return countryList;
package sample;
InvalidCountryException(String msg){
super(msg);
package sample;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
try {
countryDao.addCountry(country);
System.out.println(countryDao.getCountryList());
} catch (InvalidCountryException e) {
System.out.println(e);
package sample;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/*
* Sample Input
* 12/12/2020
* Output
* 2020-12-12
* Sample Input
* asas
* Invalid date
* 78/78/9087
* Invalid date
*/
String inputDate=input.nextLine();
if(inputDate.matches("[0-9]{2}/[0-9]{2}/[0-9]{4}")) {
SimpleDateFormat sdf=new
SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try {
date=sdf.parse(inputDate);
sdf=new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(date));
} catch (ParseException e) {
System.out.println("Invalid Date");
}else{
System.out.println("Invalid Date");
String str="aAekjiimnbohu";
/*
*5
*/
str=str.toLowerCase();
str=str.replaceAll("[^aeiou]", "");
Map<Character,Integer> hm=
new LinkedHashMap<Character,Integer>();
char c=str.charAt(i);
if(hm.containsKey(c)) {
int cnt=hm.get(c);
cnt++;
hm.put(c, cnt);
}else {
hm.put(c, 1);
}
}
System.out.println(hm);
package sample;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
class Main{
new java.io.File("D:\\capgtutorial\\tutorial\\src\\
sample\\inputfile.txt"));
while(input.hasNextLine()) {
String s=input.nextLine();
if(s.toLowerCase().replaceAll("[^aeiou]",
"").length()==2)
System.out.println(s);
}
package sample;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
// Output
Layerd Architecture
To perform CRUD
package sample;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CountryDao {
if (country.getCountryName().matches("[a-zA-Z ]{1,}"))
countryList.add(country);
else
return countryList;
return countryList.stream()
.filter(c->c.getCountryName().equals(name))
.collect(Collectors.toList()).get(0);
3.Service Layer
package sample;
import java.util.List;
if(country.getCountryName().matches("[a-zA-Z ]{1,}")) {
cdao.addCountry(country);
return cdao.getCountryList();
4.Presentation Layer
package sample;
try {
countryService.addCountry(country);
country=new Country("C002","Russia");
countryService.addCountry(country);
System.out.println(countryService.getCountry());
} catch (InvalidCountryException e) {
// TODO Auto-generated catch block
e.printStackTrace();