Professional Documents
Culture Documents
Lab-9 Harsh Singh
Lab-9 Harsh Singh
A lab Submitted
in Partial Fulfilment of the Requirements
for the Degree of
Bachelor of Technology/BTech (CSE)
characteristics.
Member variables
Member method • void setName(String name) - The method should trigger an exception
• void setAge(int age) - The method should trigger an exception InvalidAgeException if the
Also, override toString method in a class to return name and age of employee separated by a
single space.
Input 300 57
where,
Output
InvalidName InvalidAge.
#CODE
import java.util.*;
class Main
try
catch(Exception e)
System.out.println("Invalid Name");
if(age>50)
try
catch(ArithmeticException e)
System.out.println("Invalid Age");
m.setName(Name);
m.setAge(age);
}
}
#OUTPUT
Ques 2: Given a string S, find the length of string and create and raise the
custom exception NotGoodLengthException” when the length of the string is less than 10.
Write a function public int solution (String S) that accepts a string S. The function should return the
length of string or throw the NotGoodLengthException exception if the length is less than 10.
Input
Terminate
Output
NotGoodLengthException.
#CODE
import java.util.*;
class Main
void solution(String S)
{
if(S.length()<10)
try
catch(Exception e)
System.out.println("NotGoodLengthException");
else
{System.out.println(S.length());}
String S= sc.nextLine();
m.solution(S);
#OUTPUT
Ques 3: Create a class MyException with following characteristics. Data members: int numbers[] –
to store 10 integer numbers. Initialize the array with value 0.
If number and index are valid, then set the number at given index of the array. getNumber(String
index) method
#CODE
import java.util.*;
int numbers[];
MyException ()
numbers[i] = 0;
{
int i = Integer.parseInt (index);
if (i > 9 || i < 0)
numbers[i] = n;
if (i > 9 || i < 0)
return numbers[i];
String test[][] = {
{"0", "12"},
{"4", "34"},
{"6", "10"},
{"13", "4"},
{"5", "abc"}
};
for (int i = 0; i < test.length; i++)
try
} catch (Exception e)
test[i][1]);
System.out.println ();
try
myException.getNumber (test[i][0]));
} catch (Exception e)
#OUTPUT