Question

What are the results of the program as written? What results would the program produce if...

  1. What are the results of the program as written?
  2. What results would the program produce if you changed the value of a from 0 to 3?

*************************************************************************************
* Program Summary:   
*   This code includes simple try/catch (exception handling) functionality.
* We put some arithmetic code into a "try" block so that we can "catch" an exception
* that is thrown during the course working through the arithmetic.
* We need to know specifically what kind of exception we intend to catch.
*
* In this example, we suspect that at some point, one of our variables might hold
* a zero. (In a real-world situation, we might change this code so that a user
* specifies the variable values, and a user might type in zero.)
* This is a problem, because we know we are including the division operator. Dividing
* a number by zero is not possible (mathematically, the result is undefined),
* so we need to be able to handle an exception of type ArithmeticException.
* If we had other code in our try block that threw some other kind of exception,
* our catch block would not handle it. We would need to write another catch block
* to handle another kind of exception.
************************************************************************************/
package prg420week5_analyzeassignment;


public class PRG420Week5_AnalyzeAssignment {

public static void main(String args[]){
int a,b,c;       // We define variables of type integer namned a, b, and c.
try {           // We set up the try check block. We will be looking for an invalid value for integer c
a=0;       // Now we assign values to the variables.
b=15;
c=b/a;       // The result of dividing a number by 0 is undefined. Exception!
System.out.println("This line will not be executed when a=0 because when a=0, the previous line throws an exception to the catch block.");
}
catch(ArithmeticException e) { // As soon as an exception is thrown in the "try" block, execution of this code begins.
System.out.println("Catching this exception thrown by the try block: " + e);
}
System.out.println("Execution resumes here after both the try and catch blocks execute.");
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. What are the results of the program as written?

Ans: as a is zero. so c=b/a will throw an arithmetic exception. So After this statement (c = b/a), control will transfer to catch block. after printing statement in the catch block, it will print "Execution resumes here after both the try and catch blocks execute." this statement on console

Output

============================================================================================

============================================================================================

Q2: What results would the program produce if you changed the value of a from 0 to 3?

Ans: c=b/a = 15/3 = 5

there is no exception. so it will execute full try block and then go to last print statement of code.

Output

============================================================================================

Add a comment
Know the answer?
Add Answer to:
What are the results of the program as written? What results would the program produce if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...

    /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR NAME HERE * Class: PRG/420 * Creation Date: TYPE TODAY'S DATE HERE ************************************************************************************* * Program Summary: * This program converts a given date to a string. * The code includes exception handling for a ParseException. ************************************************************************************/ package prg420week5_codingassignment; import java.util.*; // wildcard to import all the util. classes import java.text.*; // wildcard to import all the text classes public class PRG420Week5_CodingAssignment { public static...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • JAVA Question 11 (20 points) Using your choice of language, C# or Java (no pseudocode allowed),...

    JAVA Question 11 (20 points) Using your choice of language, C# or Java (no pseudocode allowed), give a format/template of Exception Handling codes with try, catch, and finally blocks. In the try block include some statements that raise an arithmetic exception and the catch block catches that arithmetic exception. You don't need to write any complete method or program, just write the code segments with try, catch, and finally blocks. O Paragraph BI U Question 12 (2 points)

  • Given the ExceptionOriginal Class, run this class for Run 1plugin values 12 and 5, Run 2...

    Given the ExceptionOriginal Class, run this class for Run 1plugin values 12 and 5, Run 2 plugin values 24 and 0, Run 3 plugin values 2e and view the exceptions thrown, if any. Now modify the ExceptionOriginal class and place a test restricting user from dividing by o --- use if.... else block and display appropriate messages. Run 1 plugin values 12 and 5, Run 2 plugin values 24 and 0, and view the exceptions thrown, if any. Now modify...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • write a program to handle an exception that is generated when a program attempts to write...

    write a program to handle an exception that is generated when a program attempts to write beyond the end of an array in a lower scope such that that the exception is handled at a higher scope. java please write simple code so I can understand. use the code below public class Main { public static void main(String [] args) { int [] values = {1,2,3,4}; try { int theSum= calculate_sum(values); } catch (expection arrayindexoutofBoundsexpection) { //System.out.println(“tired to access array...

  • JAVA CODE This program causes an error and crashes. Compile and give it a test run,...

    JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • 12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user...

    12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user will enter 2 numbers and attempt to divide (in a try block) the 1st number by the 2nd number. The program will provide for 2 different catch statements: Division by zero - denominator is zero Division results in a negative number The user should be prompted with Enter 2 numbers: For input of 7 and 0 the output should be Exception: Division by zero...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT