Chapter 13
Which of the following statements is true? A. The throw statement is used to throw an exception. B. The throw statement is used to specify that a method will throw an exception. C. The throw statement is used to access an exception parameter. D. All of the above.
A. The throw statement is used to throw an exception.
After the last catch block, the required finally block provides code that always executes regardless of whether or not an exception occurs. (TRUE/FALSE)
FALSE
If a finally block appears after the last catch block, it is executed only if an exception is thrown. (TRUE/FALSE)
FALSE
If a finally block appears after the last catch block, it is executed only if an exception is thrown. (TRUE/FALSE)
FALSE
After an exception has occurred and a stack trace has been printed, the program may exit or continue executing, depending on the circumstances. (TRUE/FALSE)
TRUE
Each Exception should have three constructors: A default constructor, one that receives a string, and one that receives a string and an exception. (TRUE/FALSE)
TRUE
Each Exception should have three constructors: A default constructor, one that receives a string, and one that receives a string and an exception. (TRUE/FALSE)
TRUE
Which of the following statements is false? A. C# 6's null-conditional operator (?!) accesses a property or calls a method only if its left operand is not null; otherwise, evaluation of the expression terminates and the value of the entire expression is null. B. A nullable type is a value type that also can be null. C. You specify a nullable type by following a value type's name with a question mark (?). D. The GetValueOrDefault method checks whether a nullable-type variable contains a value. If so, the method returns that value; otherwise, it returns the value type's default value.
A. C# 6's null-conditional operator (?!) accesses a property or calls a method only if its left operand is not null; otherwise, evaluation of the expression terminates and the value of the entire expression is null. Ans: C# 6's null-conditional operator (?!) accesses a property or calls a method only if its left operand is not null; otherwise, evaluation of the expression terminates and the value of the entire expression is null. Actually, the null-conditional operator is the ?. operator, not ?!.
Which of the following is false regarding the throw point of an exception? A. It specifies the point at which the exception must be handled. B. It's the initial point at which the exception occurs. C. It's specified as the top row of the method-call stack at the time the exception occurred. D. All of the above statements are true.
A. It specifies the point at which the exception must be handled.
In C#, after an exception is handled, control resumes _________. This is known as the _________ model of exception handling. A. after the last catch block, termination B. after the last catch block, resumption C. just after the throw point, termination D. just after the throw point, resumption
A. after the last catch block, termination
After a finally block has finished executing: A. control proceeds to the first statement after the finally block B. control returns to the throw point. C. the app exits. D. control proceeds to the first statement after the last catch block.
A. control proceeds to the first statement after the finally block
When an exception occurs, the try block _______. A. expires B. continues until a matching catch block C. continues until the end of the try block D. None of the above
A. expires
The catch handler that catches an exception of type Exception should be A. last B. anywhere C. first D. a catch handler that catches an exception of type Exception should not be used
A. last
Which of the following statements is false? A. Exception handling enables programmers to write robust and fault-tolerant programs. B. Exception handling can catch the exception but cannot resolve the exception. C. Exception handling can resolve exceptions. D. There are many ways of handling exceptions.
B. Exception handling can catch the exception but cannot resolve the exception.
In the catch block below, what is e? catch (DivideByZeroException e) { Console.WriteLine(e); } A. The type of the exception being caught B. The name of catch block's exception parameter C. A finally block D. An exception handler
B. The name of catch block's exception parameter
Programmer-defined exception classes typically should contain 3 constructors: A. a default constructor, a constructor that receives a string for the error message and a constructor that receives an Exception argument of the inner exception object B. a default constructor, a constructor that receives a string and a constructor that receives both a string and an exception C. a constructor that receives a string, a constructor that receives an exception and a constructor that receives both D. a default constructor, a constructor that receives a string and a constructor that receives a number indicating the line number where the exception occurred
B. a default constructor, a constructor that receives a string and a constructor that receives both a string and an exception
Programmer-defined exception classes typically should contain 3 constructors: A. a default constructor, a constructor that receives a string for the error message and a constructor that receives an Exception argument of the inner exception object B. a default constructor, a constructor that receives a string and a constructor that receives both a string and an exception C. a constructor that receives a string, a constructor that receives an exception and a constructor that receives both D. a default constructor, a constructor that receives a string and a constructor that receives a number indicating the line number where the exception occurred
B. a default constructor, a constructor that receives a string and a constructor that receives both a string and an exception
Programmer-defined exception classes typically should contain 3 constructors: A. a default constructor, a constructor that receives a string for the error message and a constructor that receives an Exception argument of the inner exception object B. a default constructor, a constructor that receives a string and a constructor that receives both a string and an exception C. a constructor that receives a string, a constructor that receives an exception and a constructor that receives both D. a default constructor, a constructor that receives a string and a constructor that receives a number indicating the line number where the exception occurred
B. a default constructor, a constructor that receives a string and a constructor that receives both a string and an exception
An exception is: A. a problem a computer has during construction B. a problem that a program has during runtime C. something that the computer does not understand D. the way a computer signals to the users that it is about to terminate
B. a problem that a program has during runtime
In C#, try blocks are used for: A. testing for infinite loops. B. testing for exceptions. C. resolving exceptions. D. testing code
B. testing for exceptions.
In order to display the error message generated by an exception, you use: A. the Message method of class Exception B. the Message property of class Exception C. the ErrorMessage method of class Exception D. the ErrorMessage property of class Exception
B. the Message property of class Exception
Consider the following code segment: { var exampleObject = new ExampleClass(); try { exampleObject.SomeMethod(); } finally { if (exampleObject != null) { exampleObject.Dispose(); } } } Which of the following using statements is equivalent to the preceding code segment A. try using (var exampleObject = new ExampleClass()) { exampleObject.SomeMethod(); // do something with exampleObject } B. using (var exampleObject = new ExampleClass()) { exampleObject.SomeMethod(); // do something with exampleObject } C. using (var exampleObject = new ExampleClass()) { exampleObject.SomeMethod(); // do something with exampleObject exampleObject.Dispose(); } D. try using (var exampleObject = new ExampleClass()) { exampleObject.SomeMethod(); // do something with exampleObject exampleObject.Dispose(); }
B. using (var exampleObject = new ExampleClass()) { exampleObject.SomeMethod(); // do something with exampleObject }
User-defined exceptions should be derived from the: A. ApplicationException class. B .ConsoleException class. C. Exception class. D. SystemException class.
C. Exception class.
What's the difference between a try block and a try statement? A. There is no difference; the terms can be used interchangeably. B. A try statement refers to the block of code following the keyword try, while the try block refers to the try keyword and the block of code following this keyword. C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement. D. The try statement refers to the keyword try followed by a block of code. The try statement and its corresponding catch and/or finally clauses together form a try block.
C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement.
What's the difference between a try block and a try statement? A. There is no difference; the terms can be used interchangeably. B. A try statement refers to the block of code following the keyword try, while the try block refers to the try keyword and the block of code following this keyword. C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement. D. The try statement refers to the keyword try followed by a block of code. The try statement and its corresponding catch and/or finally clauses together form a try block.
C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement
What's the difference between a try block and a try statement? A. There is no difference; the terms can be used interchangeably. B. A try statement refers to the block of code following the keyword try, while the try block refers to the try keyword and the block of code following this keyword. C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement. D. The try statement refers to the keyword try followed by a block of code. The try statement and its corresponding catch and/or finally clauses together form a try block.
C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement.
In order to tell the user what happened in an exception you must A. pop the exception B. toss the exception C. access Exception properties D. throw the exception
C. access Exception properties
A catch block that does not specify an exception type or an identifier ____________. A. is an error B. cannot catch any exceptions C. can catch any exceptions D. None of the above
C. can catch any exceptions
A catch block that does not specify an exception type or an identifier ____________. A. is an error B. catch any exceptions C. can catch any exceptions D. None of the above
C. can catch any exceptions
A catch block that does not specify an exception type or an identifier ____________. A. is an error B. is an error cannot catch any exceptions C. can catch any exceptions D. None of the above
C. can catch any exceptions
User-created exceptions can be created by: A. overriding the Error class. B. overriding the Exception property. C. extending class Exception. D. they cannot be created
C. extending class Exception.
An uncaught exception: A. is a possible exception that never actually occurs during the execution of the program. B. is an exception that occurs for which the matching catch clause is empty. C. is an exception that occurs for which there are no matching catch clauses. D. is another term for a thrown exception.
C. is an exception that occurs for which there are no matching catch clauses.
If an exception is thrown in a catch handler, any code in the handler that follows the thrown exception will: A. generate a syntax error B. generate a logic error C. never be executed D. run after the finally block is done
C. never be executed
If an exception is thrown in a catch handler, any code in the handler that follows the thrown exception will: A. generate a syntax error B. generate a logic error C. never be executed D. run after the finally block is done
C. never be executed
A FormatException is used to handle: A. extra spaces input by the user B. errors with deleting a file from disk C. wrong data type inputs D. all of the above
C. wrong data type inputs
A FormatException is used to handle: A. extra spaces input by the user B. errors with deleting a file from disk C. wrong data type inputs D. all of the above
C. wrong data type inputs Ans: False. If a finally block appears after the last catch block, it is executed regardless of whether or not an exception is thrown.
Exceptions can occur: A. from C#'s CLR B. through explicitly mentioned code in a try block C. through calls to other methods made in a try block D. All of the above
D. All of the above
Which of the following statements is false? A. The HasValue property returns true if a nullable-type variable contains a value; otherwise, it returns false. B. The Value property returns the nullable-type variable's underlying value. C. The null coalescing operator (??) has two operands. If the left operand is not null, the entire ?? expression evaluates to the left operand's value; otherwise, it evaluates to the right operand's value. D. All of the above statements are true.
D. All of the above statements are true.
Which of the following statements is false? A. Prior to C# 6, you could catch an exception based only on its type. B. C# 6's exception filters enable you to catch an exception based on a catch's exception type and a condition that's specified with a when clause. C. You also can specify an exception filter for a general catch clause that does not provide an exception type. D. If an exception filter's condition is false, the exception will not be caught and the original exception's stack-trace information will be deleted.
D. If an exception filter's condition is false, the exception will not be caught and the original exception's stack-trace information will be deleted. Ans: If an exception filter's condition is false, the exception will not be caught and the original exception's stack-trace information will be deleted. Actually, if an exception filter's condition is false, the exception will not be caught and the original exception's stack-trace information will be preserved until the exception is caught.
Which of the following statements is false? A. Typically resource-release code should be placed in a finally block to ensure that a resource is released, regardless of whether there were exceptions when the resource was used in the corresponding try block. B. The using statement simplifies writing code in which you obtain a resource, use the resource in a try block and release the resource in a corresponding finally block. C. A file-processing app could process a file with a using statement to ensure that the file is closed properly when it's no longer needed. D. The resource in a using statement must be an object that implements the IDiscardable interface.
D. The resource in a using statement must be an object that implements the IDiscardable interface
Which of the following statements is false? A. Typically resource-release code should be placed in a finally block to ensure that a resource is released, regardless of whether there were exceptions when the resource was used in the corresponding try block. B. The using statement simplifies writing code in which you obtain a resource, use the resource in a try block and release the resource in a corresponding finally block. C. A file-processing app could process a file with a using statement to ensure that the file is closed properly when it's no longer needed. D. The resource in a using statement must be an object that implements the IDiscardable interface.
D. The resource in a using statement must be an object that implements the IDiscardable interface. Ans: The resource in a using statement must be an object that implements the IDiscardable interface. Actually, the object must implement the IDisposable interface.
If an exception handler is to catch a variety of exceptions, use a comma- separated list of catch arguments in the catch handler. (TRUE/FALSE)
FALSE
If an exception handler is to catch a variety of exceptions, use a comma-separated list of catch arguments in the catch handler. (TRUE/FALSE)
FALSE
In many cases, handling an exception allows a program to automatically restart execution of the program from the beginning. (TRUE/FALSE)
FALSE
Variables local to a try block are usable in the corresponding catch and finally blocks. (TRUE/FALSE)
FALSE
The finally block is executed only if no error was reached in the try block. (TRUE/FALSE)
FALSE - Ans: False, the finally block will execute no matter what; error or no error.
C# does not guarantee that a finally block (if one is present) will be executed if a try block is exited via a return, break or continue statement. (TRUE/FALSE)
FALSE - Ans: False. C# guarantees that a finally clause (if one is present) will execute if a try block exits by using a return, break or continue statement.
Resource de-allocation should be done explicitly in the finally block. (TRUE/FALSE)
TRUE
Runtime exceptions can usually be fixed by eliminating coding errors. (TRUE/FALSE)
TRUE
The StackTrace property keeps track of all the method calls that occur in a program, and that's how a program knows which method caused the error. (TRUE/FALSE)
TRUE
The base class for all exception classes is System.Exception. (TRUE/FALSE)
TRUE
The constructor method for an exception class should be overloaded to allow the customizing of the display message. (TRUE/FALSE)
TRUE
The constructor method for an exception class should be overloaded to allow the customizing of the display message. (TRUE/FALSE)
TRUE
The finally block is an ideal location for code that releases resources to prevent "resource leaks." (TRUE/FALSE)
TRUE
The finally block is an ideal location for code that releases resources to prevent "resource leaks." (TRUE/FALSE)
TRUE
There can be no code in between try/catch/finally blocks. (TRUE/FALSE)
TRUE