📄 ch07.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</HEAD>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT> <META NAME="Author" Content="Bryan Flores"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself CORBA In 14 Days -- Ch 7 --Using Exceptions to Perform Error Checking</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself CORBA In 14 Days</FONT></H1></CENTER><CENTER><P><A HREF="../ch06/ch06.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch08/ch08.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1><FONT COLOR="#000077">Day 7<BR>Using Exceptions to Perform Error Checking</FONT><A HREF="#Heading1"></A></H1></CENTER><UL> <LI><A HREF="#Heading1">Defining Exceptions for the Application</A> <UL> <LI><A HREF="#Heading2">Exceptions in BankServer</A> <LI><A HREF="#Heading3">Exceptions in Bank</A> <LI><A HREF="#Heading4">Exceptions in Account</A> <LI><A HREF="#Heading5">Exceptions in CheckingAccount</A> <LI><A HREF="#Heading6">Exceptions in SavingsAccount</A> <LI><A HREF="#Heading7">Exceptions in Customer</A> </UL> <LI><A HREF="#Heading8">Modifying Server IDL to Use Exceptions</A> <LI><A HREF="#Heading9">Modifying Server Code to Throw Exceptions</A> <UL> <LI><A HREF="#Heading10">BankServerImpl</A> <LI><A HREF="#Heading11">AccountImpl</A> <LI><A HREF="#Heading12">CheckingAccountImpl</A> <LI><A HREF="#Heading13">SavingsAccountImpl</A> <LI><A HREF="#Heading14">BankImpl</A> </UL> <LI><A HREF="#Heading15">Modifying Client Code to Catch Exceptions</A> <LI><A HREF="#Heading16">Running the Enhanced Example</A> <LI><A HREF="#Heading17">Summary</A> <LI><A HREF="#Heading18">Q&A</A> <LI><A HREF="#Heading19">Workshop</A> <UL> <LI><A HREF="#Heading20">Quiz</A> <LI><A HREF="#Heading21">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1><FONT COLOR="#000077"></FONT></H1></CENTER><P>On Day 6, you created a basic CORBA application from a set of IDL specifications.The application implemented some basic functionality but lacked robustness; for example,the client component of the application demonstrated that it was possible to withdrawfrom an <TT>Account</TT> an amount greater than the <TT>Account</TT>'s balance (withoutany sort of overdraft capability). In this chapter, you'll enhance the applicationto better handle error conditions (such as attempting to withdraw too many fundsfrom an <TT>Account</TT>).<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Defining Exceptions for the Application</FONT></H2><P>To intelligently add error-checking capability to the application, you need togo back through the design, look at each method, determine what error conditionscan occur in each method, and determine whether the error condition should be handledby the method itself or thrown (<I>raised</I> in CORBA lingo) back to the clientto be handled. In this section, you'll analyze each IDL interface again, determiningwhat exceptions can be thrown and from where.<BLOCKQUOTE> <P><HR><HR></P> <P><HR><B>Note:</B>When a method throws an exception back to its caller, CORBA refers to this action as <I>raising</I> an exception. However, many languages--particularly Java and C++--refer to this as <I>throwing</I> an exception. Because both terms have identical meaning and are commonly used, you will see them used interchangeably throughout this chapter. <HR></BLOCKQUOTE><H3><A NAME="Heading2"></A><FONT COLOR="#000077">Exceptions in BankServer</FONT></H3><P>You'll start by adding exception-handling capability to the <TT>BankServer</TT>interface. For your review (and convenience), Listing 7.1 contains the original <TT>BankServer.idl</TT>from Day 6, which you'll then modify with the exception-handling definitions.<H4><FONT COLOR="#000077">Listing 7.1. Original BankServer.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServer.idl</TT><TT> 2: </TT><TT> 3: #ifndef BankServer_idl</TT><TT> 4: #define BankServer_idl</TT><TT> 5: </TT><TT> 6: #include "Bank.idl"</TT><TT> 7: </TT><TT> 8: // A BankServer provides clients with visibility to Bank objects.</TT><TT> 9: interface BankServer {</TT><TT>10: </TT><TT>11: // Register the given Bank with this BankServer. The Bank will</TT><TT>12: // be listed by getBanks() until unregisterBank() is called with</TT><TT>13: // that Bank.</TT><TT>14: void registerBank(in Bank bank);</TT><TT>15: </TT><TT>16: // Unregister the given Bank from this BankServer. If the Bank</TT><TT>17: // was not previously registered, this operation does nothing.</TT><TT>18: void unregisterBank(in Bank bank);</TT><TT>19: </TT><TT>20: // Return a list of all Banks currently registered with this</TT><TT>21: // BankServer.</TT><TT>22: BankList getBanks();</TT><TT>23: };</TT><TT>24: 25: #endif</TT> </FONT></PRE><P>Starting your analysis with the <TT>BankServer</TT> interface, you can see thatthree methods can potentially throw exceptions. The first method, <TT>registerBank()</TT>(lines 11-14), can conceivably throw an exception if an attempt is made to registera <TT>Bank</TT> that is already registered. Make a note of this exception; you cancall it <TT>InvalidBankException</TT> (it is a good practice to make exception namesas self-explanatory as possible). Moving on to the next method, <TT>unregisterBank()</TT>(lines 16-18), you can see that it is possible that a <TT>Bank</TT> that was neverregistered with the <TT>BankServer</TT> can attempt to unregister. Similarly, thismethod might throw an <TT>InvalidBankException</TT>. Finally, <TT>getBanks()</TT>(lines 20-22) need not throw any exception; if no <TT>Bank</TT>s are registered withthe <TT>BankServer</TT>, it can simply return an empty <TT>BankList</TT>.</P><P>Your analysis of the <TT>BankServer</TT> interface has turned up two methods thatraise exceptions. The new signatures for these methods, modified to raise the exceptionsdescribed previously, are as follows:</P><PRE><FONT COLOR="#0066FF"><TT>void registerBank(in Bank bank)</TT><TT> raises (InvalidBankException);</TT><TT>void unregisterBank(in Bank bank)</TT><TT> raises (InvalidBankException);</TT></FONT></PRE><H3><A NAME="Heading3"></A><FONT COLOR="#000077">Exceptions in Bank</FONT></H3><P>You can now move on to the <TT>Bank</TT> interface, the next interface to whichyou'll be adding exception-handling capability. Again, the original <TT>Bank.idl</TT>listing from Day 6 reappears in Listing 7.2.<H4><FONT COLOR="#000077">Listing 7.2. Original Bank.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Bank.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Bank interface.</TT><TT> 4: interface Bank;</TT><TT> 5: </TT><TT> 6: #ifndef Bank_idl</TT><TT> 7: #define Bank_idl</TT><TT> 8: </TT><TT> 9: // sequence of Banks</TT><TT>10: typedef sequence<Bank> BankList;</TT><TT>11: </TT><TT>12: #include "Customer.idl"</TT><TT>13: #include "Account.idl"</TT><TT>14: </TT><TT>15: // A Bank provides access to Accounts. It can create an Account</TT><TT>16: // on behalf of a Customer, delete an Account, or list the current</TT><TT>17: // Accounts with the Bank.</TT><TT>18: interface Bank {</TT><TT>19: </TT><TT>20: // This Bank's name.</TT><TT>21: attribute string name;</TT><TT>22: </TT><TT>23: // This Bank's address.</TT><TT>24: attribute string address;</TT><TT>25: </TT><TT>26: // Create an Account on behalf of the given Customer, with the</TT><TT>27: // given account type ("savings" or "checking", where case is</TT><TT>28: // significant), and the given opening balance.</TT><TT>29: Account createAccount(in Customer customer, in string</TT><TT>30: accountType, in float openingBalance);</TT><TT>31: </TT><TT>32: // Delete the given Account. If the Account is not with this</TT><TT>33: // Bank, this operation does nothing.</TT><TT>34: void deleteAccount(in Account account);</TT><TT>35: </TT><TT>36: // List all Accounts with this Bank.</TT><TT>37: AccountList getAccounts();</TT><TT>38: };</TT><TT>39: 40: #endif</TT> </FONT></PRE><P>The <TT>Bank</TT> interface contains three methods: <TT>createAccount()</TT> (lines26-30), which need not throw any exceptions; <TT>deleteAccount()</TT> (lines 32-34),which throws an exception if the specified <TT>Account</TT> object does not existin the <TT>Bank</TT>; and <TT>getAccounts()</TT> (lines 36-37), which also need notthrow any exceptions.</P><P>Again using a self-explanatory exception name, the modified <TT>deleteAccount()</TT>signature looks like this:</P><PRE><FONT COLOR="#0066FF"><TT>void deleteAccount(in Account account)</TT><TT> raises (InvalidAccountException);</TT></FONT></PRE><H3><A NAME="Heading4"></A><FONT COLOR="#000077">Exceptions in Account</FONT></H3><P>Listing 7.3 contains the <TT>Account</TT> interface (again making a reappearancefrom Day 6), the next candidate for adding exception raising.<H4><FONT COLOR="#000077">Listing 7.3. Original Account.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Account.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Account interface.</TT><TT> 4: interface Account;</TT><TT> 5: </TT><TT> 6: #ifndef Account_idl</TT><TT> 7: #define Account_idl</TT><TT> 8: </TT><TT> 9: // sequence of Accounts</TT><TT>10: typedef sequence<Account> AccountList;</TT><TT>11: </TT><TT>12: #include "Customer.idl"</TT><TT>13: </TT><TT>14: // An Account is an entity owned by a Bank and held by a Customer</TT><TT>15: // (or multiple Customers). An Account has a balance which can be</TT><TT>16: // affected by deposits and withdrawals.</TT><TT>17: interface Account {</TT><TT>18: </TT><TT>19: // This Account's account number.</TT><TT>20: readonly attribute string accountNumber;</TT><TT>21: </TT><TT>22: // This Account's creation date.</TT><TT>23: readonly attribute string creationDate;</TT><TT>24: </TT><TT>25: // This Account's current balance.</TT><TT>26: readonly attribute float balance;</TT><TT>27: </TT><TT>28: // Return a list of Customers who hold this Account.</TT><TT>29: CustomerList getCustomers();</TT><TT>30: </TT><TT>31: // Withdraw the given amount from this Account. Returns the new</TT><TT>32: // account balance.</TT><TT>33: float withdraw(in float amount);</TT><TT>34: </TT><TT>35: // Deposit the given amount into this Account. Returns the new</TT><TT>36: // account balance.</TT><TT>37: float deposit(in float amount);</TT><TT>38: };</TT><TT>39: 40: #endif</TT> </FONT></PRE><P>Analyzing the <TT>Account</TT> interface's methods, in lines 28-29 you encounter<TT>getCustomers()</TT>, which need not throw any exceptions (again, an empty listcan be returned if there are no <TT>Customer</TT>s, even though this should neverhappen). Lines 31-33 contain the <TT>withdraw()</TT> method, which throws an exceptionif insufficient funds are available in the specified account. Finally, <TT>deposit()</TT>(lines 35-37) throws an exception if an invalid amount is specified (for instance,a negative amount). Actually, <TT>withdraw()</TT> throws an exception when givenan invalid amount, as well.</P><P>This analysis leads to the following method signatures:</P><PRE><FONT COLOR="#0066FF"><TT>float withdraw(in float amount)</TT><TT> raises (InvalidAmountException,</TT><TT> InsufficientFundsException);</TT><TT>float deposit(in float amount)</TT><TT> raises (InvalidAmountException);</TT></FONT></PRE><H3><A NAME="Heading5"></A><FONT COLOR="#000077">Exceptions in CheckingAccount</FONT></H3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -