Download presentation
Presentation is loading. Please wait.
Published byDustin Gregory Modified over 9 years ago
1
Exception Handling To use Try blocks to delimit code in which exceptions might occur. To use Catch blocks to specify exception handlers. To use the Finally block to release resources.
2
Uncaught exception Program mostly terminates when there is an uncaught exception ► If debugging in Visual Studio, application pauses and Exception Assistant appears indicating where the exception occurred the exception occurred
3
Dim n1, n2, r As Integer n1 = num1.Text n1 = num1.Text n2 = num2.Text n2 = num2.Text r = n1 / n2 r = n1 / n2 MessageBox.Show(r) MessageBox.Show(r) Take and assign inputs from user
4
Fig. 12.3 | Exception Assistant.
5
► Error Handling is an essential part of any good code. ► Exception An indication of a problem that occurs during a program ’ s execution System.Exception is the base class for all exceptions The coding structure VB.NET uses to deal with such Exceptions is called the Try … Catch structure.
6
Syntax Try …. Catch Try [ try statements ] Catch ExceptionVariable As ExceptionType [ catch statements ] ‘ Additional Catch block Finally [ finally statements ] End Try
7
Try Block ► Means “ Try to execute this code “ ► Encloses code that might throw an exception and the code that should not execute if an exception occurs ► Corresponding End Try ► There must be at least one Catch block and/or Finally block immediately after the Try block
8
Catch Block ► Means "Catch any errors here “ ► Catches and handles an exception Begins with keyword Catch Specifies an identifier and exception type ► Example: Catch e As Exception Executes when exception of proper type matches
9
Finally Block ► Programs that obtain certain resources must return them explicitly to avoid resource leaks ► Finally block Optional in a Try statement Placed after the last Catch block (if there is one) Executes whether or not an exception is thrown in the corresponding Try block or any of its corresponding Catch blocks Generally we use it to close an opened file or connection
10
Termination Model of Exception Handling ► When an exception occurs: Try block terminates immediately Program control transfers to first matching Catch block (other Catch block are ignored) ► After exception is handled: Termination model of exception handling ► Program control does not return to the throw point because the Try block has expired ► Flow of control proceeds to the first statement after the last Catch block Resumption model of exception handling ► Program control resumes just after throw point
11
Try n1 = num1.Text n1 = num1.Text n2 = num2.Text n2 = num2.Text r = n1 / n2 r = n1 / n2 MessageBox.Show(r) MessageBox.Show(r) Catch ex As Exception MessageBox.Show(ex.Message) MessageBox.Show(ex.Message) End Try
12
DivideByZeroTest
13
► Delimit تحصر او تحدد ► Release يحرر ► Resources المصادر ► Uncaught exception غير مسك استثناء ► Pauses مؤقتا ► Exception Assistant استثناء مساعد ► Essential ضروري ► obtain certain الحصول على بعض ► Termination Model of Exception Handling إنهاء النموذجي لمعالجة استثناء ► Program control transfers to first matching Catch block (other Catch block are ignored)
14
► نقل برنامج لمراقبة مطابقة أول كتلة الصيد ► Program control does not return to the throw point because the Try block has expired ► مراقبة البرنامج لا عودة الى رمي النقطة لأن حاول عرقلة قد انتهت صلاحيتها ► Resumption model of exception handling فرضية نموذج لمعالجة الاستثناء ► Program control resumes just after throw point ► يستأنف برنامج مراقبة فقط بعد رمي نقطة ► resumes يواصل
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.