Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.

Similar presentations


Presentation on theme: "Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also."— Presentation transcript:

1 Dr. Abraham

2 Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also under unusual conditions that stress its designers' assumptions. Fault tolerant Deal with problems and continue executing

3 Exception handling Overview Perform a task If the task did not execute correctly (exception thrown) perform error processing Perform next task If program is not fault tolerant the program terminates and prints an error message as given by the operating system.

4 Exceptions Old days, programmers used code for every possible anticipated errors. Today there are built in handlers This removes the error-handling code from main-line of the program’s execution, improving clarity of the program. You can choose to handle any exceptions There are hierarchy of exceptions. You choose what you want to handle.

5 Exception handling As soon as an exception is raised all further execution of the methods code is halted and transferred to a catchment area. Catchment area is the catch block between try and end try. Two models exist The resumption model Execution returns to the raise point The termination model

6 Handling an Error Try Program statements Example place a division by zero Catch avariableName As DivideByZerioException Show the message contained in avariableName.Message End Try

7 Example see page 582 Division by zero without exception handling Program dies and prints out the STACK TRACE Stack trace includes the exception name (system.dividebyzeroexception) and gives the method where it occoured (main()) In the second error on p 582, “hello” was entered instead of a number. A system.formatexception was thrown.

8 Catching exceptions in this example Try Result = numerator \denominator Catch aVariableName as formatException Write a message of some sort here Catch aVariableName as DivideByZeroException Write a message of some sort here End Try -make sure to spell exceptions exactly-

9 Catching Exceptions A try block may have several catch blocks Exception-handling code appears in a catch block May use a parameter to assign the caught error If no parameter is given (i.e. catch without any parameters) will catch all exceptions. Uncaught exception will open the exception assistant within the debug environment.

10 Finally Block Finally block is guaranteed to execute regardless of whether the try block executes successfully or an exception occurs. In the final block one may place resource release code such as file close, memory de-allocation, etc. Example: Finally If Not (exampleObject Is Nothing) Then exampleObject.Dispose() End if

11 Model of Exception Handling When problem is detected, an exception is thrown (the point at which error occurred is called a throw point). If the throw point is in the try block, that block is terminated and the program control transfers to the catch block and continues after the catch block. CLR tries to match an appropriate catch block if there are several.

12 Termination model of exception handling If an exception occurs in a try block, the program control is transferred to the following catch block where the exception parameter’s type matches the thrown exception. Then the control resumes after the last catch block. This is known as resumption model of exception handling or termination model of exception handling. If no exception occur, the program completes the try block and skips all catch blocks and executes the first statement following the try and catch blocks.

13 An Example program Demonstrated here

14 .NET exception Hierarchy Class Exception of namespace System is the Base class A derived class from this is the SystemException. An example is out-of-range array index. The systemException throws IndexOutOfRangeException. Other examples: OutofMemoryException, StackOverflowException, etc.


Download ppt "Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also."

Similar presentations


Ads by Google