Exception Handling Mechanism in .NET CLR


Published on

Published in: Education
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Exception Handling Mechanism in .NET CLR

  1. 1. Presented by: 08-SE-59 08-SE-64 08-SE-69 08-SE-72
  2. 2. <ul><li>Analysis of the .NET CLR Exception Handling Mechanism </li></ul><ul><li>Fruja,N.G., Börger,E. </li></ul>
  3. 3. <ul><li>Introduction to CLR </li></ul><ul><li>Exceptions </li></ul><ul><li>Exception Handling </li></ul><ul><li>Exception Handling mechanism in .NET CLR </li></ul><ul><li>Conclusion </li></ul>
  4. 4. <ul><li>Primary Parts: </li></ul><ul><ul><li>.NET Framework </li></ul></ul><ul><ul><li>Common Language Runtime (CLR) </li></ul></ul><ul><li>What is CLR? </li></ul><ul><li>The Common Language Runtime ( CLR ) is a core component of Microsoft's .NET initiative. In the CLR, code is expressed in a form of byte code </li></ul>
  5. 6. <ul><li>JIT allows code to run in a protected environment as managed code. </li></ul><ul><li>CLR also allows for enforcement of code access security. </li></ul><ul><li>Verification of type safety. </li></ul><ul><li>Improved “garbage collection” </li></ul>
  6. 8. <ul><li>The term exception is shorthand for the phrase &quot;exceptional event.&quot; </li></ul><ul><li>An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. </li></ul><ul><li>Exception can result in: </li></ul><ul><ul><li>Error occurred in execution time </li></ul></ul><ul><ul><li>Abnormal termination of program </li></ul></ul><ul><ul><li>Wrong execution result </li></ul></ul>
  7. 10. <ul><li>This class is the base class for all exceptions. </li></ul><ul><li>Two categories of exceptions exist under the base class Exception : </li></ul><ul><ul><li>The pre-defined common language runtime exception classes; derived from SystemException . </li></ul></ul><ul><ul><li>The user-defined application exception classes derived from ApplicationException . </li></ul></ul>
  8. 11. <ul><li>Exception Object: </li></ul><ul><ul><li>When there is an exception, the .NET framework creates an object of type 'Exception' and 'throws' it. </li></ul></ul><ul><ul><li>contains information about the 'error. </li></ul></ul><ul><li>throwing an exception </li></ul><ul><li>Explicit Exceptions </li></ul><ul><ul><li>throw statement. </li></ul></ul>
  9. 12. <ul><li>Syntax: </li></ul><ul><ul><li>throw exception; </li></ul></ul><ul><li>Can be used together with the try catch statement. </li></ul><ul><ul><li>The try block contains the guarded code that may cause the exception. </li></ul></ul><ul><ul><li>The catch block contains the appropriate code to handle that exception. </li></ul></ul>
  10. 13. <ul><ul><ul><ul><li><html><body> <script type=&quot;text/javascript&quot;> var x=prompt(&quot;Enter a number between 0 and 10:&quot;,&quot;&quot;); try   {   if(x>10)        throw &quot;Err1&quot;;    else if(x<0)         throw &quot;Err2&quot;;    else if(isNaN(x))         throw &quot;Err3&quot;;    } </li></ul></ul></ul></ul>
  11. 14. <ul><li>catch(er)   {   if(er==&quot;Err1&quot;)     {     alert(&quot;Error! The value is too high&quot;);     }   if(er==&quot;Err2&quot;)     {     alert(&quot;Error! The value is too low&quot;);     }   if(er==&quot;Err3&quot;)     {     alert(&quot;Error! The value is not a number&quot;);     }   } </script> </body> </html> </li></ul>
  12. 15. <ul><li>Inner Exception Property: </li></ul><ul><ul><li>The most current exception called inner exception. </li></ul></ul><ul><ul><li>stored in the inner exception property. </li></ul></ul><ul><li>Outer Exception Property: </li></ul><ul><ul><li>The outer exception is thrown in response to this inner exception. </li></ul></ul><ul><li>The code that handles the outer exception can use the information from the earlier inner exception. </li></ul>
  13. 16. <ul><li>Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions , special conditions that change the normal flow of program execution. </li></ul><ul><li>e.g. In , .NET try, catch </li></ul><ul><li>are used to handle exceptions. </li></ul>
  14. 17. <ul><li>Termination model </li></ul><ul><li>Retry model </li></ul><ul><li>Resumption model </li></ul>
  15. 18. <ul><li>First popular mechanism </li></ul><ul><li>The code after that point, at which an exception occurs, will not be executed. </li></ul><ul><ul><li>i.e. the execution is terminated at the point at which an exception occurs </li></ul></ul>
  16. 19.
  17. 20. <ul><li>The handler will try to clean up the environment resulted from the exception and will try to restart the execution. </li></ul><ul><li>The original environment at that point of execution should be restored. </li></ul>
  18. 21. Restart point This part of the code will be executed again
  19. 22. <ul><li>The handler does the work of cleaning up the damage resulted because of the exception </li></ul><ul><li>Then control is transferred to the point next to the one where the execution flow was interrupted because of the exception and the execution continues from there on. </li></ul>
  20. 23. Exception point and restart point
  21. 25. <ul><li>A catch handler that can handle any type of exception. </li></ul><ul><li>A finally handler is executed under any circumstance, regardless whether an exception occurs or not and can be used to close critical resources such as files or handles. </li></ul><ul><li>A fault handler is called whenever an exception occurs but is not called as part of the normal control flow. </li></ul><ul><li>A filtered handler handles exceptions of a specified type (or subtype of the specified type) and is only called when such an exception was thrown. </li></ul>
  22. 26. <ul><li>The exception handling mechanism excCLR proceeds in three passes: </li></ul><ul><li>The StackWalk PASS </li></ul><ul><li>The Unwind PASS </li></ul><ul><li>The Leave PASS </li></ul>
  23. 27. <ul><li>There are three groups of possible handlers in a given frame during its StackWalk: </li></ul><ul><li>a catch/filter handler whose try block protects the program counter of the frame pointed by stackCursor . </li></ul><ul><li>a filter handler whose filter region contains pc of the frame pointed at by stackCursor .This corresponds to an outer exception. </li></ul>
  24. 28. <ul><li>When Unwinding, the EXC CLR searches for: </li></ul><ul><li>the matching target handler , i.e. the handler determined at the end of the Stack Walk pass (if any) – handler can be undef if the search in the Stack Walk has been exited because the exception was thrown in a filter region. </li></ul><ul><li>a matching finally/fault handler whose associated try block protects the pc. </li></ul><ul><li>a handler whose handler region contains pc. </li></ul><ul><li>a filter handler whose filter region contains pc . </li></ul>
  25. 29. <ul><li>In the Leave pass, the EXC CLR machine searches for: </li></ul><ul><li>F inally/catch/filter handlers that are “on the way” from the pc to the target. </li></ul>
  26. 31. <ul><li>Exception is an event, that interrupts the normal flow of the program's instructions </li></ul><ul><li>Exception handling is a mechanism to handle the occurrence of exceptions. </li></ul><ul><li>.NET CLR handles exceptions using PASSES. </li></ul>
  27. 32. <ul><li>The StackWalk Pass, Unwind Pass and Leave Pass work together to provide the exception handling mechanism in CLR. </li></ul>