Exception handler


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Exception handler

  1. 1. Exception Handler
  2. 2. Questions # 1 <ul><li>Define Exception, exception handler, raising an exception, disabling an exception, continuation and built in exception. </li></ul>
  3. 3. Answer <ul><li>Exceptions are errors detected by hardware, such as disk read errors and unusual conditions such as end files. These are also unusual events , erroneous or not, that is detectable either by hardware or software and that may require special processing. </li></ul>
  4. 4. Answer <ul><li>Exception Handler is a code unit that monitors the special processing that may be required by the detection of an exception or also called exception handling. </li></ul><ul><li>Raising an Exception An object raises an exception when it has trapped an error. An object raises an exception by sending itself the &quot;raiseException&quot; message, passing an error number as a parameter. </li></ul>
  5. 5. Answer <ul><li>Continuation is the question of control continuation after handler execution for an exception handler executes either control can transfer to somewhere in the program outside of the hadler code, or program execution can simply terminate. </li></ul><ul><li>Built in Exception is an exception that is constructed as part of the language and is not detachable. </li></ul>
  6. 6. Question # 2 <ul><li>What are the design issues for an exception handling? </li></ul>
  7. 7. Answer <ul><li>1. Form – a choice between having handlers that are complete programs units or handlers that are code segments. They may be embedded in the units that caused the exception they are to handle or they may be embedded in a different unit, such as the unit that called the one in which the exception is raised. </li></ul>
  8. 8. Answer <ul><li>2. How an exception occurrence is bound to an exception handler – This occurs in two different levels, On the unit level, there is the question of how the same exception being raised at different points in a unit can be bound to different handlers within the unit. It should be possible to bind the exception that can be raised by particular statements to particular handlers, even though the same exception can be raised by many different statements. At the higher level, the binding question arises when there is no exception handler local to the unit in which the exception is raised. If exceptions are propagated, a single handler might handle the same exceptions raised in several program units, which may require the handler to be more general than one would prefer. </li></ul>
  9. 9. Answer <ul><li>3. Whether the binding of exceptions to handlers is static or dynamic , that is , whether binding depends on the syntactic layout of the program or on its execution sequence. As it is in other language constructs, static binding of exceptions is easier to understand and implement than dynamic binding. </li></ul>
  10. 10. Answer <ul><li>4. If users are allowed to define exceptions, how are there exceptions specified? The usual answer is to require that they be declared in the specifications parts of the program units in which they can be raised. </li></ul><ul><li>5. Whether built in exceptions can be raised explicitly by the user program. This can be convenient if there are software – detectable situations in which the user would like to use a built- handler. </li></ul>
  11. 11. Answer <ul><li>6. Whether hardware-detectable errors should be treated as exceptions that could be handled by user program. It not, all exceptions obviously are software-detectable. A related question is whether there should be any built in exceptions. </li></ul><ul><li>7. Whether exception, either built in or user-defined, can be temporarily or permanently disabled. </li></ul>
  12. 12. Question # 3 <ul><li>What does it mean for an exception to be bound to an exception handler? </li></ul>
  13. 13. Answer <ul><li>If the block or unit in which an exception is raised does not have a handler for that exception, the exception is propagated elsewhere to be handled – Procedures - propagate it to the caller – Blocks - propagate it to the scope in which it appears – Package body - propagate it to the declaration part of the unit that declared the package (if it is a library unit, the program is terminated) </li></ul>
  14. 14. Answer <ul><li>– Task - no propagation; if it haandler, execute it; in either case, mark it &quot;completed --- Sa JAVA Binding an exception to a handler is simpler in Java than it is in C++ – An exception is bound to the first handler with a parameter is the same class as the thrown object or an ancestor of it • An exception can be handled and rethrown by including a throw in the handler (a handler could also throw a different exception) </li></ul>
  15. 15. Question # 4 <ul><li>What is the parent class of most Java user-defined exception classes? </li></ul>
  16. 16. Answer <ul><li>Throwable class. Based on the book, All Java exceptions are objects of classes that are descendants of the Throwable class </li></ul>
  17. 17. Question # 5 <ul><li>How can an exception handler be written in Java so that it handles any exceptions? </li></ul>
  18. 18. Answer <ul><li>To ensure that exceptions can be thrown in a try clause are always handled in a method, a special handler can be written that matches all exceptions that are derived from Exception simply by defining the handler with an Exception type parameter as in catch (Exception genericObject) { … } </li></ul>
  19. 19. Question # 6 <ul><li>What is the difference between a C++ throw specifications and a Java throws clause? </li></ul>
  20. 20. Answer <ul><li>1. A C++ program can throw any type defined in the program or by the system. In Java, only objects that are instances of Throwable or some class that descends from it can be thrown. </li></ul><ul><li>2. A C++ program unit that does not include a throws clause can throw any exception, which tells the reader nothing. A Java method that does not include throws clause cannot throw any checked exceptions that it does not handle. Therefore, the reader of a Java method knows from its header what exceptions it could throw but does not handle. </li></ul>
  21. 21. Answer <ul><li>3. The addition of the finally clause is great convenience in certain situations. It allows cleanup kinds of actions to take place regardless of how a compound statement terminated. </li></ul><ul><li>4. The Java run-time system implicitly throws a variety of exceptions, such as for array indices out of range and null reference variable accesses, which can be handled by any user program. A C++ program can handle only those exceptions that it explicitly throws. </li></ul>
  22. 22. Question # 7 <ul><li>What is the difference between checked and unchecked exceptions in Java? </li></ul>
  23. 23. Answer <ul><li>Checked exceptions are carried out or executed whenever there is an error in the syntax and programming-side while unchecked exceptions hold the errors that might occur during user or data interaction. </li></ul>
  24. 24. Question # 8 <ul><li>Can you disable a Java exception? </li></ul>
  25. 25. Answer <ul><li>There are exceptions in Java that are automatically invoked when a data-specific error occurs. These exceptions are also called checked exceptions. They cannot be disabled as they are built-in with the Java API. But if you are using an unchecked exception, you may disable it intentionally or unintentionally by using a break; statement just before a line that might throw an exception. </li></ul>
  26. 26. Question # 9 <ul><li>What is the purpose of the Java finally clause? </li></ul>
  27. 27. Answer <ul><li>Purpose: To specify code that is to be executed, regardless of what happens in the try construct </li></ul>
  28. 28. <ul><li>Kristine Ancheta </li></ul><ul><li>Stephanie Rosanna Blanza </li></ul><ul><li>Desney Ann Caracot </li></ul><ul><li>Roshe Nikhka de Guzman </li></ul><ul><li>Rhys Anton Gabion </li></ul><ul><li>Erick Lester Sumugat </li></ul><ul><li>Carlo Mar </li></ul>