SLC S22W4 | Exception Handling in Java

in dynamicdevs-s22w4 •  7 hours ago  (edited)

Black Gold Elegant Jewelry Logo_20250110_075402_0000.png

edited in canva


Hello friends and welcome to my article in the SLC S22W4 as organized by the great administrator @kouba01 I would be definitely participating in the tasks assigned immediately.


TASK 1: Tasked with managing temperature data collected from multiple sensors in a factory. Sensor sends invalid data ( e.g negative temperatures for sensor meant to measure above freezing temperature) Write a program to process a list of temperature reading and handling exceptions for invalid values

Program detects a faulty sensor, logging error messages and tries to process the rest of readings. List of temperature readings are created and also a list to hold valid readings. We loop through each elements, invalid readings, log error message as well as know. Using try-catch block, program stands to be able to handle invalid reading, logs the error and continue running.

Screenshot_20250109-234127.pngcode✅Screenshot_20250109-233850.pngoutput✅
TASK 2: An online registration from accepts participants for an event, age must vary between 18 and 60. Write program validating input throwing custom exceptions. InvalidAgeException for out-of-range ages. Program should catch exception and display message explaining why Input was valid.

Program main goal is validate participant ages, log error for ages out of ranges. We create a method using InvalidAgeException which allows show clear message when input is valid using loops we repeat process to get user's age in the validAge method to source fo invalidAgeException The program checks age of less than 18 or greater than 60 as demanded and thus create this program

Screenshot_20250109-235105.pngcode✅Screenshot_20250109-212933.pngoutput✅
TASK 3: Develop simple RPG ( Role Playing Game) players encounter error as insufficient health point, invalid moves or missing inventory items. Design hierarchy of custom exceptions ( e.g Health Exception, More Exception Inventory Exception and write program to handle them at appropriate levels depending on content

Handling mistakes use different creating logic to handle different errors, for special problems. Using Health Exception when health is low More Exception to spot wrong moves. InventoryExceptionwhen items are missing. These exception would definitely be for all existing exception.

We use handle player movement, the move string action method will check if movement is in wrong direction to throw MoreException and if health print insufficient levy less or equal to zero to throw Inventory Exception At this point, we check for valid. A success message highlights and shows moved method to next method use string item for players to use item. If players doesn't have item. InventoryException will show error.

Screenshot_20250110-000726.pngScreenshot_20250110-000019.png

You could notice that it didn't display error because it had inventory incorporated into the program


TASK 4: Banking system, user transfer money between accounts create program validates transaction , handling exceptions showing InsufficientFundsException and InvalidAccountException and Negative TransferException Ensure program provides detailed feedback to user and log errors for failed transactions.

Bank Account quarter handles operation like withdrawal and deposits. Banking system class to manage to add account and transfer operations in bank system. To notify if senders account has enough funds. We intentionally create program for 456DC5 to 8750CE with varying balance of 400 and 500 respectively testing diverse transactions.

Screenshot_20250110-002706.pngScreenshot_20250109-214748.png

TASK 5: Write program that opens multiple files, reads their content and close them properly even if an exception occurs during the reading process. Use 'finally' block or any other mechanism to ensure resource cleanup

We create program that opens multiple files, read content and close them. We use try block whole ensuring the final block for cleanup is achieved, we use try to handle file related exceptions. We loop to iterate through spotted file in array. Using buffered reader for reading file line array line by line. If file doesn't exist the IOException is thrown. Block make sure file is close, no matter if exception is thrown during reading. Check if Buffered Reader Object is not null and print immediately.

Screenshot_20250110-004645.pngcode✅Screenshot_20250110-004958.pngoutput✅

TASK 6: Design program that proves user activity logs for e-commerce website. If a user attempts to log in with incorrect credentials more than 3 toms consecutively, throw a Suspicious Activity Exception `Ensure program records the activity for further analysis and alert administrator

Creating program to count log incorrect login attempt throwing SuspiciousActivityException to handle suspicious login attempts. This happens when case exceeds 3. We store numbers of failed attempt too. We decisively insert incorrect password. If statement check failed count and prints message. Login failed for user if a password is correct, failed attend count to reset to 0 the username and failed attempts would be visibly seen.

Screenshot_20250110-005404.pngcode✅Screenshot_20250110-005731.pngoutput✅

I have duly performed these tasks, it took a while to make out these work.

I have concluded my article and invite @enamul17 @dove11 @solperez @fantvwiki to join challenge
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!