0% found this document useful (0 votes)
3 views1 page

Assignmentvii Nitjsr Java

The document outlines a series of lab assignments for a B.Tech. (CSE) course focused on Object-oriented Programming using Java. It includes tasks such as handling exceptions, creating custom exceptions, and implementing a class with specific properties and behaviors. The assignments emphasize exception handling techniques and the importance of proper program flow, even when errors occur.

Uploaded by

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

Assignmentvii Nitjsr Java

The document outlines a series of lab assignments for a B.Tech. (CSE) course focused on Object-oriented Programming using Java. It includes tasks such as handling exceptions, creating custom exceptions, and implementing a class with specific properties and behaviors. The assignments emphasize exception handling techniques and the importance of proper program flow, even when errors occur.

Uploaded by

ncsupply007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Assignments – VII

[Link]. (CSE) Semester II


Object-oriented Programming using Java Lab (CS1205)

1. In main method, invoke another method of your choice, that will perform some division by zero.
Do not catch the exception. Note down the stack trace printed by JRE. Now, catch the
corresponding exception, and print the exception string from within the catch clause. Program
must not terminate in this second scenario.

2. Surround the try block with another catch clause of your choice, say
ArrayIndexOutOfBoundException. Access some out of bound array index in your catch clause.
Swap the sequence of previous method call and array access, and observe which catch clause
gets invoked.

3. Create a checked exception named InvalidEntryException, that will take a String as a parameter
in its constructor. When caught and printed as object, i.e., upon invocation of
[Link](e) it must print “InvalidEntryException:” followed by the String. But when
[Link]() will be invoked, it must omit the “InvalidEntryException:” part and return only
the String. From main method, throw and catch the exception. Within catch clause, write the two
lines [Link]([Link]()); followed by [Link](e) and test your
exception.

4. Create a class named Person, with properties name, and age. It must have a constructor taking
name and age as parameters, that will set the instance variables properly. The constructor will
throw the above created exception if number less than 0 is passed via age parameter. The class
must have two getter methods each for the two properties, as well. Create some Person objects in
main, with some age parameter set less than 0 to check the program.

5. Modify the InvalidEntryException to incorporate the cause of the exception as well, i.e., it will
have a constructor with two parameters, firstly the String mentioned as it is, and another
Throwable object which will be set as its cause within the constructor. Modify the methods
created within the Exception to return a modified message that will include the cause as well,
i.e., printing the Exception object should now print “InvalidEntryException:” followed by the
String, followed by “caused by” and the cause of the Exception that was passed as second
parameter. Modify the Person class to incorporate an IllegalArgumentException(“invalid age
provided”) as cause of the thrown exception. Check your outputs appropriately.

6. Irrespective of whether exception is thrown or not, a string stating “Person objects created” must
always be printed in main, after creating the Person objects. Modify the code accordingly. Now,
change the InvalidEntryException into an unchecked exception. What modifications do you
need to perform in your solutions to the above mentioned problems?

You might also like