This chapter is from the book

Apply Your Knowledge

Exercises

3.1 Handling Exceptions

Recall Step By Step 2.5 ("Common Dialog Boxes") from Chapter 2. This Step By Step demonstrated the use of common dialog boxes by creating a simple rich text editor. This editor allows you to open and save a rich text file. You can edit the text or change its fonts and colors. The program worked fine in all cases unless you tried to open or save a file that was already open; in this case the program throws a System.IO.IOException.

The objective of this exercise is to make a more robust version of this program that generates a warning about the open file rather than abruptly terminating the program.

Estimated Time: 15 minutes.

Create a new Visual Basic .NET Windows application.

Add a Windows Form to the project. Name this form Exercise3_1.

Place five Button controls to the form. Name them btnOpen, btnSave, btnClose, btnColor, and btnFont and change their Text property to &Open..., &Save..., Clos&e..., &Color..., and &Font..., respectively. Place a RichTextBox control and name it rtbText. Arrange all controls as shown in Figure 3.12.

3.2 Validating User Input

One of the techniques for input validation is to force the user to fix an erroneous field before allowing her to move to another field. To achieve this, you can set the Cancel property of the CancelEventArgs argument of the Validating event of a field to True.

In this exercise, you'll create a login form (see Figure 3.14) that accepts a username and a password. It forces the user to enter the username. The user should be also able to close the application by clicking the Cancel button irrespective of the validation status of the fields.

Estimated Time: 15 minutes.

Add a new form to your Visual Basic .NET application.

Place three Label controls, two TextBox controls named txtUserName and txtPassword, two Button controls named btnLogin and btnCancel and an ErrorProvider component named ErrorProvider1 on the form. The ErrorProvider component will be placed in the component tray. Arrange the controls in the form as shown in Figure 3.14.

Run the project. If you click directly on the Login button, you will be forced to enter a username. However, you can click the Cancel button to close the application even without entering a valid username.

Review Questions

What is the default behavior of the .NET framework when an exception is raised?

Which is the base class of all the exceptions that provides the basic functionality for exception handling? What are the two main types of exception classes and their purposes?

Explain the Message and InnerException properties of the Exception class.

What is the purpose of Try and Catch blocks?

How many Catch blocks can be associated with a single Try block? How should they be arranged?

What is the importance of the Finally block?

Can you associate custom error messages with the exception types defined by the CLR? If yes, how can you do this?

What are some of the points you should consider before creating custom exceptions?

What is the importance of the Validating event?

Explain the purpose of the ErrorProvider component.

Exam Questions

You are creating a data import utility for a personal information system that you designed recently. When the record in the source data file is not in the required format, your application needs to throw a custom exception. You will create an exception class with the name InvalidRecordStructureException. Which of the following classes would you choose as the base class for your custom exception class?

ApplicationException

Exception

SystemException

InvalidFilterCriteriaException

You are assisting your colleague in solving the compiler error that his code is throwing. The problematic portion of his code is

You need to debug a program containing some exception handling code. To understand the program better, you created a stripped down version of it and included some MessageBox statements that give clues about the flow of its execution. The program has the following code:

The Validating event of a text box in your Windows application contains the following code. The MyValidatingCode method validates the contents of the text box. If the contents are invalid, this method throws an exception and retains the focus in the text box. The line numbers in the code sample are for reference purpose only.

You have designed a Windows Form that works as a login screen. The form has two TextBox controls named txtUserName and txtpassword. You want to ensure that the user can only enter lowercase characters in the controls. Which of the following methods should you use?

Set the form's KeyPreview property to True and program the KeyPress event of the form to convert uppercase letters to lowercase letters.

Create a single event handler attached to the KeyPress event of both txtUserName and txtpassword. Program this event handler to convert the uppercase letters to lowercase.

Use the CharacterCasing property of the controls.

Use the Char.ToLower method in the TextChanged event handlers of the controls.

You must create a custom exception class in your Windows application. You have written the following code for the exception class:

Derive the exception class from the base class SystemException instead of ApplicationException.

In your Windows application, you have created a dialog box that allows the user to set options for the application. You have also created a Help button that the user can press to get help on various options in the dialog box. You validate the data entered by the user in a TextBox control labeled Complex Script; if the user enters an invalid value in this text box, you set the focus back in the control by setting the cancel property of the CancelEventArgs object to True. While testing the application, you discovered that once you enter invalid data in the text box, you could not click on the Help button without correcting the data first. What should you do to correct the problem?

Set the CausesValidation property of the text box to False.

Set the CausesValidation property of the text box to True.

Set the CausesValidation property of the Help button to False.

Set the CausesValidation property of the Help button to True.

You are writing exception handling code for an order entry form. When the exception occurs, you want to get information about the sequence of method calls and the line number in the method where the exception occurs. Which property of your exception class can help you?

HelpLink

InnerException

Message

StackTrace

Your code uses the Throw statement in this fashion:

Catch e As Exception
Throw

Which of these statements is true about this code?

The Throw statement catches and rethrows the current exception.

The Throw statement catches, wraps, and then rethrows the current exception.

The Throw statement must be followed by an exception object to be thrown.

The Throw statement transfers control to the Finally block following the Catch block.

You are creating a Windows Form that works as a login screen for an Order Entry system designed for the sales department of your company. Which of the following strategies should you follow?

Design a ValidateUser method. Throw a new custom exception named EmployeeNotFound when the entered username is not in the database.

Design a ValidateUser method. Throw an ArgumentException exception when the user types special characters in the User name or Password text boxes.

Design a ValidateUser method. It returns True if the username and password are correct; otherwise it returns False.

Design a ValidateUser method. Throw an ApplicationException when the entered username is not in the database.

You want to capture all the exceptions that escape from the exception handling code in your application and log them to Windows event log. Which of the following techniques would you use?

Write all the code of the application inside a Try block, attach a generic Catch block to that Try block, and handle the exception there.

Write all the code of the application inside a Try block, attach a Catch block that catches the super type of all exceptions (the Exception objects), and write code to make entries in the event log there.

Program the ProcessExit event handler of the AppDomain class.

Program the UnhandledException event handler of the AppDomain class.

Which of the following is generally the most robust way to record the unhandled exceptions in your application?

Create an entry in Windows event log.

Create an entry in the application's custom event log.

Create an entry in a table in Microsoft SQL Server 2000 database.

Send an email using SMTP.

The structured exception handling mechanism of the .NET Framework allows you to handle which of the following types of exceptions? (Select all that apply.)

The code executes successfully but the value of the variable result is truncated.

Answers to Review Questions

The .NET framework terminates the application after displaying an error message when an exception is raised.

The Exception class is the base class that provides common functionality for exception handling. The two main types of exceptions derived from Exception class are SystemException and ApplicationException. SystemException represents the exceptions thrown by the common language runtime, whereas ApplicationException represents the exceptions thrown by user code.

The Message property describes the current exception. The InnerException property represents an exception object associated with the exception object; this property is helpful when a series of exceptions are involved. Each new exception can preserve information about a previous exception by storing it in InnerException property.

The Try block encloses code that might raise an exception. The Catch block handles any exception raised by the code in the Try block.

Zero or more Catch blocks can be associated with a single Try block. If no Catch block is associated with a Try block, a Finally block should follow the Try block; otherwise a compile-time error occurs. The Catch blocks should be arranged from specific to general exception types to avoid a compile-time error.

The code contained by the Finally block always executes regardless of any exception in the application. Therefore, you can use the Finally block to write cleanup code to complete tasks, such as closing data connections, closing files, and so on, that need to be performed regardless of whether an exception occurred.

Yes, you can associate custom error messages with the exception classes defined by the Common Language Runtime to provide more meaningful information to the caller code. The constructor of these classes that accepts the exception message as its parameter can be used to pass the custom error message.

Custom exceptions should be derived from ApplicationException and should be created only if existing classes do not meet the requirements of your application. They should have a name ending with the word Exception and should implement the three constructors (default, Message, and Message and Exception) of their base class.

The Validating event is the ideal place to store the field-level validation logic for a control. The Validating event handler can be used to cancel the event if validation fails, thus forcing the focus to the control. This forces the user to enter correct data.

The ErrorProvider component in the Visual Studio .Net toolbox can be used to show validation-related error icons and error messages to the user.

Answers to Exam Questions

A. When creating a class for handling custom exceptions in your programs, the best practice is to derive it from the ApplicationException class. The SystemException class is for system-defined exceptions. The Exception class is the base class for both ApplicationException and SystemException classes and should not generally be derived from directly.

C. When you have multiple Catch blocks associated with a Try block, you must write them in order from the most specific to the most general one. The Catch block corresponding to the ArithmeticException should come at the end because it is a more general class than the three (DivideByZeroException, NotFiniteNumberException and the OverFlowException) classes derived from it.

B. When an exception occurs in a Try block, it will search for a matching Catch block associated with that Try block. In this case, the ArithmeticException generates a match for DivideByZeroException because DivideByZeroException is derived from ArithmeticException, and the exception is handled right there. In all cases the Finally block is executed.

D. The message box displays System.ArgumentOutOfRangeException because you caught and wrapped that exception in the InnerException property of the exception caught later by the outer Catch block.

A. When you want to retain the focus inside a control after the Validating event is processed, you must set the Cancel property of the CancelEventArgs argument in the Validating event, so the correct answer is e.Cancel = True. The CausesValidation property has a different purpose: It decides whether a Validating event will be fired for a control.

C. The CharacterCasing property when set to CharacterCasing.Lower for a text box will convert all uppercase letters to lowercase as you type them. It is the preferred way to enforce either lowercase or uppercase input in a text box.

A, C. The best practices for exception handling recommend that you end the name of your exception class with the word Exception. In addition, an exception class must implement three standard contructors. The missing constructor is the one given in option C.

C. When you want a control to respond regardless of the validation statuses of other controls, set the CausesValidation property of that control to True. Then the Help button should have its CausesValidation property set to False.

D. The StackTrace property of the Exception class and the classes that derive from it contains information about the sequence of method calls and the line numbers in which the exception occurred. Therefore, it is the right property to use.

A. The Throw statement just catches and throws the current exception.

C. It is obvious that user can make typing mistakes while typing her username or password. You should not throw exceptions for these situations; you should rather design a ValidateUser method that returns a result indicating whether the login was successful.

D. To capture all unhandled exceptions for an application, you must program the UnhandledEvent event handler of its AppDomian class.

A. Logging to the Windows event handler is the most robust solution because the other solutions have more assumptions that can fail. Sometimes your application can loose connectivity with the database or with the SMTP server, or you might have problems writing an entry in a custom log file.

A, B, C, D. The .NET Framework allows you to handle all types of exceptions including cross-language exceptions for both CLS- and nonCLS-complaint languages. It also allows you to handle exceptions from unmanaged code, both COM and nonCOM.

C. Since you are multiplying two maximum possible values for integers, the result cannot be stored inside an integer. The compiler will detect it and will flag a compile-time error.