exception handling to use try blocks to delimit code in which exceptions might occur. to use catch...

14
Exception Handling Exception Handling To use Try blocks to delimit code in To use Try blocks to delimit code in which which exceptions might occur. exceptions might occur. To use Catch blocks to specify To use Catch blocks to specify exception handlers. exception handlers. To use the Finally block to release To use the Finally block to release resources. resources.

Upload: dustin-gregory

Post on 27-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Exception HandlingException Handling

To use Try blocks to delimit code in which To use Try blocks to delimit code in which exceptions might occur.exceptions might occur.

To use Catch blocks to specify exception To use Catch blocks to specify exception handlers.handlers.

To use the Finally block to release To use the Finally block to release resources.resources.

Uncaught exceptionUncaught exception

Program mostly terminates when there is Program mostly terminates when there is an uncaught exceptionan uncaught exception

►If debugging in If debugging in Visual StudioVisual Studio, application , application pauses pauses

and and Exception AssistantException Assistant appears indicating appears indicating wherewhere

the exception occurredthe exception occurred

Dim n1, n2, r As IntegerDim n1, n2, r As Integer

n1 = num1.Textn1 = num1.Text

n2 = num2.Textn2 = num2.Text

r = n1 / n2r = n1 / n2

MessageBox.Show(r)MessageBox.Show(r)

Take and assign Take and assign inputs from userinputs from user

Fig. 12.3Fig. 12.3 | | Exception AssistantException Assistant..

►Error Handling is an essential part of Error Handling is an essential part of any good code.any good code.

►ExceptionException An indication of a problem that occurs An indication of a problem that occurs

during a programduring a program’’s executions execution System.ExceptionSystem.Exception is the base class for all is the base class for all

exceptionsexceptions The coding structure VB.NET uses to deal The coding structure VB.NET uses to deal

with such Exceptions is called the with such Exceptions is called the Try Try …… CatchCatch structure. structure.

Syntax Try Syntax Try ……. Catch. Catch

TryTry[ try statements ][ try statements ]

CatchCatch ExceptionVariable ExceptionVariable AsAs ExceptionTypeExceptionType[ catch statements ][ catch statements ]

‘‘Additional Catch blockAdditional Catch blockFinallyFinally

[ finally statements ][ finally statements ]

End TryEnd Try

TryTry Block Block

► Means “ Try to execute this code “Means “ Try to execute this code “

► Encloses code that might throw an exception Encloses code that might throw an exception and the code that should not execute if an and the code that should not execute if an exception occursexception occurs

► Corresponding End TryCorresponding End Try

► There must be There must be at least at least oneone Catch block Catch block and/orand/or Finally block immediately after the Try blockFinally block immediately after the Try block

CatchCatch Block Block

► Means "Catch any errors hereMeans "Catch any errors here““

► Catches and handles an exceptionCatches and handles an exception Begins with keyword CatchBegins with keyword Catch Specifies an identifier and exception type Specifies an identifier and exception type

►Example: Catch e As ExceptionExample: Catch e As Exception Executes when exception of proper type Executes when exception of proper type

matchesmatches

FinallyFinally Block Block

► Programs that obtain certain resources must Programs that obtain certain resources must return them explicitly to avoid resource leaksreturn them explicitly to avoid resource leaks

► Finally blockFinally block Optional in a Try statementOptional in a Try statement Placed after the last Catch block (if there is Placed after the last Catch block (if there is

one)one) Executes whether or not an exception is Executes whether or not an exception is

thrown in the corresponding Try block or any thrown in the corresponding Try block or any of its corresponding Catch blocksof its corresponding Catch blocks

Generally we use it to close an opened file or Generally we use it to close an opened file or connectionconnection

Termination Model of Exception Termination Model of Exception HandlingHandling

► When an exception occurs:When an exception occurs: Try block terminates immediatelyTry block terminates immediately Program control transfers to first matching Catch Program control transfers to first matching Catch

block (other Catch block are ignored)block (other Catch block are ignored)► After exception is handled:After exception is handled:

Termination model of exception handlingTermination model of exception handling►Program control does not return to the throw Program control does not return to the throw

point because the Try block has expiredpoint because the Try block has expired►Flow of control proceeds to the first statement Flow of control proceeds to the first statement

after the last Catch blockafter the last Catch block Resumption model of exception handling Resumption model of exception handling

►Program control resumes just after throw pointProgram control resumes just after throw point

TryTry n1 = num1.Textn1 = num1.Text n2 = num2.Textn2 = num2.Text r = n1 / n2r = n1 / n2 MessageBox.Show(r)MessageBox.Show(r)CatchCatch ex ex AsAs Exception Exception MessageBox.Show(ex.Message)MessageBox.Show(ex.Message)End TryEnd Try

DivideByZeroTest

►DelimitDelimit تحصر او تحددتحصر او تحدد►ReleaseReleaseيحرر يحرر ►ResourcesResourcesالمصادر المصادر ►Uncaught exceptionUncaught exception استثناء مسك استثناء غير مسك غير►PausesPauses مؤقتا مؤقتا►Exception AssistantException Assistant مساعد مساعد استثناء استثناء►EssentialEssential ضروري ضروري►obtain certainobtain certainالحصول على بعض الحصول على بعض ►Termination Model of Exception Termination Model of Exception

HandlingHandling استثناء لمعالجة النموذجي استثناء إنهاء لمعالجة النموذجي إنهاء►Program control transfers to first matching Catch Program control transfers to first matching Catch

block (other Catch block are ignored)block (other Catch block are ignored)

الصيد ► كتلة أول مطابقة لمراقبة برنامج الصيد نقل كتلة أول مطابقة لمراقبة برنامج نقل►Program control does not return to the throw point Program control does not return to the throw point

because the Try block has expiredbecause the Try block has expired

ألن ► النقطة رمي الى عودة ال البرنامج ألن مراقبة النقطة رمي الى عودة ال البرنامج مراقبةصالحيتها انتهت قد عرقلة صالحيتها حاول انتهت قد عرقلة حاول

►Resumption model of exception Resumption model of exception handlinghandling االستثناء لمعالجة نموذج االستثناء فرضية لمعالجة نموذج فرضية

►Program control resumes just after throw pointProgram control resumes just after throw point

نقطة ► رمي بعد فقط مراقبة برنامج نقطة يستأنف رمي بعد فقط مراقبة برنامج يستأنف►resumesresumes يواصل يواصل