Java Day 7
Java Day 7
Java Day 7
compile type error is mandatory to handle bcz it is crucial to handle at time of compilation
exception is propagated to the method which has called it if it is not able to handle it.
Throw:- throw exception explicitly (means we can throw any exception any time anywhere)
final: it is a keyword used to define a method or variable which can’t be changed throughout
the program
finally: we use finally {} after try and catch method to run the rest code whether try catch occurs
or not
finalize: finalize() method is called by garbage collector and performs the cleaning with respect to
the object before its destruction.
2)Demostrate use of throws in exception handling
package exceptionHandling;
/*Demostrate use of throws in exception handling*/
public class HandlingDemo{
public static int divide(int a,int b) throws ArithmeticException{
int sum=0;
try {
sum=a/b;
}catch(ArithmeticException e) {
System.out.println("b cant be 0");
}
return sum;
}
}
3)Take names of 5 students display them using foreach loop
After that ask use to search for a student
Enter the name to search:priya
if present display found at position 3rd[2nd index]position
otherwise not found
package array;
public class StudentSearch {
4)create a array of prices of products and find the product with highest price and lowest price
package array;
/*create a array of prices of products and find the product with highest price
and lowest price*/
public class Product {
}
5)Create 3 customer objects and store it into a arraylist
customer with id,name,age
and sort it by its age
}
public class CustomerArrayListDemo {
}
6) Multiple Catch
package exceptionPractice;
import java.io.FileWriter;
import java.io.IOException;
Collection framework:
Classes
Interface
Algorithm
Linkedlist:- list< data type > ll= new LinkedList< data type >(); or
LinkedList< data type > ll= new LinkedList< data type >();
Collection:- it is an interface
2. Comparater:-
Lab book:
Exercise 1: Validate the age of a person and display proper message by using user defined exception.
Age of a person should be above 15.
Exercise 2: Write a Java Program to validate the full name of an employee. Create and throw a user
defined exception if firstName and lastName is blank.
import java.util.Scanner;
try {
int salary=sc.nextInt();
if(salary<3000) {
throw new EmployeeException(salary);
}
}catch(EmployeeException e) {
System.out.println("Increase the salary");
}catch(Exception e) {
System.out.println("There is some error. Handle that");
}
System.out.println("bye bye");