Question

In: Computer Science

1) Describe briefly what exceptions are and what their purpose is. What is the try block?...

1) Describe briefly what exceptions are and what their purpose is. What is the try block? What is the catch block? What happens with the flow of control if the catch block does not catch a thrown exception? Give a simple example.

2) Describe briefly what templates are and what their purpose is.

3) Write one line of code for each line below. What is the template type in each case?

- Create a vector to store student IDs
- Create a vector to store student names
- Create a vector to store Point2D points
- Create a set to store unique (no duplicates) student names
- Create a set to store unique (no duplicates) student IDs
- Create a map that maps student IDs to their score
- Create a map that maps student IDs to their name
- Create a map that maps student names to the name of their lab partner

Solutions

Expert Solution

1)Answer:

  • Exceptions are events that occur during the execution of programs that disrupt the normal flow of instructions (e.g. divide by zero, array access out of bound, etc.).
  • In Java, an exception is an object that wraps an error event that occurred within a method and contains:

    • Information about the error including its type
    • The state of the program when the error occurred
    • Optionally, other custom information
  • Exception objects can be thrown andcaught.

Exceptions are used to indicate many different types of error conditions.

  • JVM Errors:

    • OutOfMemoryError
    • StackOverflowError
    • LinkageError

System errors:

  • FileNotFoundException
  • IOException
  • SocketTimeoutException

    • Programming errors:
  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • ArithmeticException
  • Exceptions separate error handling code from regular code.

    • Benefit: Cleaner algorithms, less clutter
  • Exceptions propagate errors up the call stack.

    • Benefit: Nested methods do not have to explicitly catch-and-forward errors (less work, more reliable)
  • Exception classes group and differentiate error types.

    • You can group errors by their generalize parent class, or
    • Differentiate errors by their actual class
  • Exceptions standardize error handling.

In a traditional programming language like C, an error condition is usually indicated using an integer return value (e.g. -1 for out of memory). However, this practice:

  • Requires standardization on error codes (hard for large projects)
  • Makes functions hard to use because they must return actual values by reference
  • Requires programmers to check return error codes in every nested function call. This leads to cluttered source code and hard-to-follow logic
  • Does not allow for the state of the program to be easily captured on an error condition for later examination
  • Cannot be enforced, and programmers often ignore error conditions, which leads to security or stability issues in program.

Try block:

The try block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must followed by a Catch block or Finally block or both.

Catch block:

catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType , declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name.

2)Templates:

template is a form, mold, or pattern used as a guide to making something. Here are some examples:

  • A ruler is a template when used to draw a straight line.
  • A document in which the standard opening and closing parts are already filled in is a template that you can copy and then fill in the variable parts.
  • An overlay that you put on your computer keyboard telling you special key combinations for a particular application is a template for selecting the right keys to press.
  • Flowcharting templates (not used much now) help programmers draw flowcharts or logic sequences in preparation for writing the code.

Related Solutions

C++ problem 1) Describe briefly what exceptions are and what their purpose is. What is the...
C++ problem 1) Describe briefly what exceptions are and what their purpose is. What is the try block? What is the catch block? What happens with the flow of control if the catch block does not catch a thrown exception? Give a simple example. 2) Describe briefly what templates are and what their purpose is.
For practice using exceptions, this exercise will use a try/catch block to validate integer input from...
For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...
What are the six exceptions to the basic requirements of FASB ASC 740? Briefly describe each...
What are the six exceptions to the basic requirements of FASB ASC 740? Briefly describe each of the exceptions.
1. Briefly describe the purpose/role of): Transformation of bacteria with ligation reactions. What specifically did this...
1. Briefly describe the purpose/role of): Transformation of bacteria with ligation reactions. What specifically did this step allow for and how is the step important to the overall sequencing analysis? 2. Does a 15 nucleotide deletion alter the protein sequence?
Briefly describe what is meant by ABC analysis. What is the purpose of this inventory technique?...
Briefly describe what is meant by ABC analysis. What is the purpose of this inventory technique? Discuss the major inventory costs that are used in determining the EOQ.
What is the purpose of the income statement? Briefly identify and describe the major types of...
What is the purpose of the income statement? Briefly identify and describe the major types of expenses that are shown on the typical income statement. Please don't hand write. Thank you.
This java program try to count all of the 1 at the rim of the block...
This java program try to count all of the 1 at the rim of the block of 1 surrounded by 0. The total of the count also includes all the 1 surrounded two pockets of 0 within the block. The correct answer fro the total count is 154. However, this program gives 167. Where is the fault of the program?    public class Cloture {     public static void main(String[] args) { int[][] carte = {                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0},                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},...
What exceptions to the "hearsay rule" have courts recognized? Describe the reasoning behind these exceptions. 300+...
What exceptions to the "hearsay rule" have courts recognized? Describe the reasoning behind these exceptions. 300+ words
What is a block cipher? What is the equation to describe a block cipher?
 What is a block cipher? What is the equation to describe a block cipher? What is symmetric key encryption and compare with Public key encryption? Why is a block cipher considered a keyed permutation? What is the most common block cipher currently in use? Explain the "key schedule" Considering the DES algorithm, what Caused it to encounter serious security issues? What are Sub-Bytes? What is the difference in output when considering ShiftRows and MixColumns? What is 3DES? 3DES is the workhorse of which industry and why?
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are it's benefits? What does it mean when we raise a custom exception?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT