Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

In C#, the catch clause takes an exception class name, and a variable to use in referring to this class. The block is executed if one of the statements in the try block throws this class as an exception. The catch clause can leave this class name out to catch any exception. Here, we catch all Exception class objects, which is generally all exceptions in .NET. For example, the OpenFileDialog.OpenFile method can throw a file I/O exception using the IOException class. Since this class derives from the Exception class, it will be caught by our handler. Other exceptions such as OutOfMemoryException may also occur, and are caught by our block as well.3

HANDLE EXCEPTIONS IN THE BTNLOAD_CLICK METHOD

 

Action

Results and Comments

 

 

 

4

Handle the exception by

One way to do this is as follows:

 

displaying a message to the user.

catch (Exception ex)

 

 

 

Note: In this case, we return

{

 

// Handle exception

 

to the caller without loading

 

MessageBox.Show(

 

an image.

"Unable to load file: " + ex.Message);

 

 

}

 

 

 

This code uses a class we have not seen before: the MessageBox class. This class is used to display a simple dialog box. We discuss this class in detail in chapter 8. For now, just copy the code and trust me.

The Message property for the ex variable is used in our dialog to insert the message string describing the exception provided by the Exception object. This and other members of the Exception class are summarized in .NET Table 2.1.

We will use exceptions throughout the book to handle errors in a similar manner. Other concepts associated with exceptions will be presented as they are required by our sample program.

Our MyPhotos application is now in line with our MyForm application from section 1.3, with the slight improvement of handling any exception that occurs while opening the file. Our last task in this chapter is to enable the form to resize gracefully using the Anchor property.

2.4Resizing forms

Our final task in this chapter is to set the behavior for resizing using the Anchor property for our controls, and establish a minimum size for the form so that our PictureBox control does not disappear. This will finish our duplication of the application created in chapter 1. The remainder of this book will use Visual Studio

.NET when discussing applications changes, so we will carry the MyPhotos application into chapter 3 and beyond.

3 A more formal discussion of exceptions and the exception handling syntax appears in appendix A.

RESIZING FORMS

61

Figure 2.6

The application is resized here via the PictureBox.Anchor property.

.NET Table 2.1 Exception class

The Exception class represents a generic exceptional condition, and serves as the base class for all exception classes in .NET. This class is part of the System namespace, and provides information required to raise (throw) and process (catch) exceptions. Note that it is possible for unmanaged code to throw exceptions that will not be seen as Exception objects. These exceptions can be caught using an empty catch clause.

 

HelpLink

Gets a link to help information associated with this

 

 

exception.

 

InnerException

Gets the inner (nested) exception associated with this

 

 

object, if any.

 

Message

Gets the message text assigned to the exception.

Public

Source

Gets or sets a string containing the source of the

Properties

 

exception, such as the name of the application or object

 

 

 

 

that generated the error.

 

StackTrace

Gets the stack trace as a string. By default, the stack is

 

 

captured just before the exception is thrown.

 

TargetSite

Gets the MethodBase object for the method that threw

 

 

this exception.

 

 

 

 

GetBaseException

Returns the original Exception that caused the current

 

 

exception to be thrown. Useful when a chain of nested

 

 

exceptions is received.

Public

SetHelpLink

Sets the string returned by the HelpLink property.

Methods

 

 

 

ToString

Returns the fully qualified name of the exception, and

 

(overridden from

possibly other information such as the message text, the

 

Object)

name of the inner exception, and a stack trace.

 

 

 

62

CHAPTER 2 GETTING STARTED WITH VISUAL STUDIO .NET

Соседние файлы в папке c#