The following example shows how a programmer might avoid a division by zero error by explicitly checking for the error condition:if(gallonsOfGas != 0)mpg = milesDriven /gallonsOfGas;else mpg = 0;Another possible way to deal with this is to use exception handling mechanisms. What factors help the programmer decide which of these methods to use?

What will be an ideal response?


The example code represents the most efficient method of handling the error if you think it will be a frequent problem. Because a program that contains this code does not have to instantiate an Exception object every time the user enters a 0 for the value of gallonsOfGas, the program saves time and computer memory. On the other hand, if you think dividing by zero will be infrequent, then the decision will execute many times when it is not needed. In other words, if a user enters 0 for gallonsOfGas in only one case out of 1,000, then the if statement is executed unnecessarily 999 times. In that case, it is more efficient to eliminate the if test and instantiate an Exception object when needed.
Another advantage to using exception handling instead of decision making to control errors is that when a program's main logic is interrupted by a series of statements and decisions that might detect errors, the program can become more complicated and difficult to understand. The object-oriented approach to error handling separates a program's main tasks from code that deals with rare exceptions.

Computer Science & Information Technology

You might also like to view...

The condition known as __________ occurs when you try to store an integer that is bigger than the maximum allowed by the number of bits you have.

Fill in the blank(s) with correct word

Computer Science & Information Technology

Which of the following statements is false?

a. When we’re talking about a group, the entire group is called the population. b. Sometimes a population is quite large, such as the people likely to vote in the next U.S. presidential election—in excess of 100,000,000 people. c. For practical reasons, polling organizations trying to predict who will become the next president work with carefully selected small subsets of the population known as samples. Many of the polls in the 2016 election had sample sizes of about 1000 people. d. Measures of dispersion help you understand how concentrated the values are.

Computer Science & Information Technology

Like Java application programs, Java applets have the method main.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

What allows transportation of all active Virtual LANs (VLAN) between switches using a single physical link?

a. VLAN trunks. b. VLAN frames. c. VLAN links. d. VLAN ports.

Computer Science & Information Technology