Java Programming, Loose-leaf Version
Java Programming, Loose-leaf Version
8th Edition
ISBN: 9781337685917
Author: Joyce Farrell
Publisher: Cengage Learning
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 12, Problem 8RQ
Program Description Answer

In object-oriented programming, the code segment that handles or takes appropriate action following an exception is a catch block.

Hence, the correct answer is option “B”.

Blurred answer
Students have asked these similar questions
Question (1): Write a program that asks the user to enter an integer greater than zero and store it in a variable called “n”. Then it calculates the final value that will be stored in a variable called “s” according to the following equation                                    And add an exception when the user enters any value other than integer shows the message “You entered non integer value, please try again.”, and let the user to enter a new value.Example 1Enter an integer: 4S = -2.0Example 2Enter an integer: aYou entered non integer value, please try again.Enter an integer: 4S = -2.0
# Split input into 2 parts: name and ageparts = input().split()name = parts[0]while name != '-1':# FIXME: The following line will throw ValueError exception.# Insert try/except blocks to catch the exception.age = int(parts[1]) + 1print('{} {}'.format(name, age))# Get next lineparts = input().split()name = parts[0]   How to fix this code with try except block
Q1) The following snippet of code inputs an integer and outputs it:int num=0;Scanner kbd = new Scanner(System.in);num = kbd.nextInt();System.out.println(num);Run this program but enter text instead of an integer. The programshould crash and tell you what kind of exception was thrown by thenextInt method. Wrap this code inside a try/catch block where youcatch the exception that is thrown. Add a loop so the user must enterthe number again if text is entered.Q2) The following class maintains an account balance and returns a special error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to deposit invalid amounts and catches the exceptions that are thrown
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT