Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
12.10. throw Keyword
This section
Practice test
11 questions on this section. Wrong answers show you what to read again.
Sign up to take itWhole chapter
Revision test
Mixed questions from across the chapter. Your answers get marked.
Sign up to take itQuick
Flashcard drill
2 cards from this lesson. Good the night before a test.
Try these first
- 1.
What is the purpose of the 'throw' keyword in Java?
Hint
Think about how you might signal that something has gone wrong in your program.
- 2.
Write a line of code using the 'throw' keyword to indicate a division by zero error.
Hint
Remember the exception type associated with arithmetic operations.
- 3.
What does the 'throw' keyword do in Java?
- Creates a new method
- Manually throws an exception
- Handles an exception
Hint
It’s about signaling errors.
- 4.
True or False: The 'throw' keyword can be used in a method declaration.
- True
- False
Hint
Think about where you place the keywords.
- 5.
Design a simple application that prompts a user for their age and throws a custom exception if the age is less than a defined threshold (e.g., 18).
Hint
Think about how you’d communicate age restrictions.
- 6.
Write a method that calculates the square root of a number and throws an exception if the input number is negative.
Hint
What happens with negative numbers in square roots?
Exercises
Total Questions
2
Estimated Time
4 min
Passing Score
70%
Instructions
- Read each question carefully
- You can use hints if you need help
- Complete all questions before submitting
4 more questions available
Enrol freeQuiz
Total Questions
2
Estimated Time
4 min
Passing Score
70%
Instructions
- Read each question carefully
- You can use hints if you need help
- Complete all questions before submitting
1 more question available
Enrol freeChallenge Problems
Total Questions
2
Estimated Time
4 min
Passing Score
70%
Instructions
- Read each question carefully
- You can use hints if you need help
- Complete all questions before submitting