In: Computer Science
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
1)Answer:
In Java, an exception is an object that wraps an error event that occurred within a method and contains:
Exceptions are used to indicate many different types of error conditions.
JVM Errors:
System errors:
SocketTimeoutException
Exceptions separate error handling code from regular code.
Exceptions propagate errors up the call stack.
Exception classes group and differentiate error types.
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:
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: