Java Exception Handling - Exercises, Practices, Solutions
Java exercises on handling and managing exceptions [7 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
Java Exceptions to handle errors:
Java programming language uses exceptions to handle errors and other exceptional events. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
This section covers how to catch and handle exceptions. It includes try, catch, and finally block, as well as chained exceptions and logging exercises.
1. Write a Java program that throws an exception and catch it using a try-catch block.
2. Write a Java program to create a method that takes an integer as a parameter and throws an exception if the number is odd.
3. Write a Java program to create a method that reads a file and throws an exception if the file is not found.
4. Write a Java program that reads a list of numbers from a file and throws an exception if any of the numbers are positive.
5. Write a Java program that reads a file and throws an exception if the file is empty.
6. Write a Java program that reads a list of integers from the user and throws an exception if any numbers are duplicates.
7. Write a Java program to create a method that takes a string as input and throws an exception if the string does not contain vowels.
Java Code Editor
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Java: Tips of the Day
IsPowerOfTwo
Checks if a value is positive power of two.
To understand how it works let's assume we made a call IsPowerOfTwo(4).
As value is greater than 0, so right side of the && operator will be evaluated.
The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.
The result of (value & value) is value. 100 & 100 => 100.
This will value the expression to true as value is equal to value.
public static boolean isPowerOfTwo(final int value) { return value > 0 && ((value & (~value + 1)) == value); }
Ref: https://bit.ly/3sA5d4I
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook