14.02.2017 Views

. Стиллмен, Дж. Грин - Изучаем C#, 2-е издание

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Возьми в руку карандаш<br />

обработка исключ<strong>е</strong>ний<br />

Вот прим<strong>е</strong>р н<strong>е</strong>работающ<strong>е</strong>го кода. Справа вы видит<strong>е</strong> сообщ<strong>е</strong>ния о пяти<br />

исключ<strong>е</strong>ниях. Укажит<strong>е</strong>, каки<strong>е</strong> строки кода стали причиной появл<strong>е</strong>ния<br />

этих сообщ<strong>е</strong>ний.<br />

p u b l i c s t a t i c v o i d B e e P r o c e s s o r {) {<br />

o b j e c t myBee = new H o n ey B ee( 3 6 .5 , " Z ip p o" );<br />

f l o a t howMuchHoney = (flo a t)m y B e e ;<br />

H oneyBee a n o th er B e e = new H o n ey B ee( 1 2 .5 , "Buzzy")<br />

d o u b le beeName = d o u b l e . P a rse(a n o th er B e e.M y N a m e);<br />

М <strong>е</strong> т о Э dowW e.ParseC'SZ )<br />

CO зн(яч<strong>е</strong>нм<strong>е</strong>м 32..<br />

d o u b le t o ta lH o n e y = 3 6 .5 + 1 2 .5 ;<br />

s t r i n g beesW eCanFeed =<br />

f o r ( i n t 1 = 1 ; i < ( i n t ) t o ta lH o n e y ; i+ + ) {<br />

}<br />

beesW eCanFeed += i . T o S t r i n g ( );<br />

f l o a t f =<br />

f l o a t . P a rse(b ee sW e C a n F e ed );<br />

i n t d r o n e s = 4;<br />

i n t q u een s = 0 ;<br />

i n t d ro n esP erQ u een = d r o n e s / q u ee n s;<br />

a n o th er B e e = n u l l ;<br />

i ^ NullReferenceException was unhandled<br />

; \ OvertlowException was unhandled ( J<br />

Vdue was ather too large or too sma# for a Single.<br />

I Object refererrce not set to m hstance o f an object<br />

t\ I nvaHdCastExceptlon was unhandled<br />

у / i f (d ron esP erQ u een < 10) {j SpecifledcastisnotvaW.<br />

[ an oth erB ee.D oM yJob O ;<br />

\ '<br />

\ Присво<strong>е</strong>ни<strong>е</strong><br />

\ ссылк<strong>е</strong><br />

знач<strong>е</strong>ния nul!<br />

означа<strong>е</strong>т^ что<br />

она никуда н<strong>е</strong><br />

в<strong>е</strong>д<strong>е</strong>т.<br />

i<br />

DivldeBvZeroException was unhandled<br />

1 Attempted to dMde by zero.<br />

Troubleshooting tips;<br />

Ea!s.§...M§_..!b§..Yalue,of.^e je n o [riia to [Js not г<strong>е</strong>го<br />

Get general help for thiis exception,<br />

, -v'<br />

Search for more Help Otiine,,.<br />

FomiatException was unhandled<br />

I Input string was not in a correct format<br />

Troubleshooting tips;<br />

[Make sure your..m e#gia!3 u m e n t ^ e Jn,,tii^<br />

When converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object,<br />

Get general help for this exception.<br />

j Search for more Help Online,,.<br />

дальш<strong>е</strong> > 451

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!