Coding Problems in Java Programming by Tawqeer ul Islam
Coding Problems in Java – Set 5
1. Handle Division by Zero
Write a program that takes two integers and performs division. Handle the case where the
denominator is zero.
Concepts: try-catch, ArithmeticException
2. Handle Invalid Array Index
Create an array of size 5 and try to access an invalid index. Handle the exception gracefully.
Concepts: ArrayIndexOutOfBoundsException
3. Handle Non-Numeric Input
Take input from the user and convert it to an integer. Handle invalid (non-numeric) input.
Concepts: NumberFormatException
4. Multiple Catch Blocks
Write a program that:
• Takes two numbers as input (strings)
• Converts them to integers
• Divides them
Handle:
• Invalid number input
• Division by zero
Concepts: multiple catch blocks
5. File Reading with Exception Handling
Read data from a file. Handle file not found and ensure file is closed properly.
Concepts: IOException, finally
Coding Problems in Java Programming by Tawqeer ul Islam
6. Using finally Block
Write a program that demonstrates that finally always executes, even if an exception occurs.
Concepts: finally behavior
7. Throw Exception for Invalid Age
Create a method that accepts age. If age < 18, throw an exception with message "Not
eligible".
Concepts: throw, custom validation
8. Banking Withdrawal System
Simulate a bank account:
• If withdrawal amount > balance → throw exception
• Otherwise deduct amount
Concepts: logical exceptions, throw
9. Create Custom Exception
Create a custom exception class InvalidPasswordException.
Condition:
• Password must be at least 8 characters and contain a digit
Concepts: user-defined exceptions
10. Student Result Processing System
Create a system that:
• Accepts marks for 5 subjects
• Throws exception if marks < 0 or > 100
• Calculates average
• Handles all exceptions properly
Concepts:
• validation
• multiple exceptions
Coding Problems in Java Programming by Tawqeer ul Islam
• clean program structure
Bonus Challenge
11. Nested Exception Handling
Write a program with:
• inner try-catch inside outer try-catch
• demonstrate how control flows