Skip to main content

Exception Handling & Event Handling

 

Q. Define exception, exception handler, and built-in exception.  

Exception: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Any unusual event, erroneous or not, that is detectable by either hardware or software and that may require special processing is called exception.

Exception Handler: An exception handler processes a raised exception. The exception can be either predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the run-time system.  
Built-in Exception: Built-in exceptions are the exceptions which are available in libraries. These exceptions are suitable to explain certain error situations. Below is the list of important built-in exceptions in Java.
  1. Arithmetic Exception
  2. ArrayIndexOutOfBoundException
  3. ClassNotFoundException
  4. FileNotFoundException
  5. IOException.

Q. What are the advantages of having support for exception handling built-in to a language?

Without built-in exception handling, the code to detect error condition can be a clutter to the program. The existence of built-in exception handling would simplify a source program.

A language that supports exception handling encourages its users to consider all of the events that could occur during program execution and how they can be handled. This approach is far better than not considering such possibilities and simply hoping nothing will go wrong.

Exception handling encourages programmers to consider many different possible errors.

Exception propagation allows a high level of reuse of exception handling code.

Q. What are the design issues of exception handling? 

The goals of exception handling mechanisms are to make programs more reliable and robust.
  1. How are user-defined exceptions specified?
  2. Should there be default exception handlers for programs that do not provide their own?
  3. Can built-in exceptions be explicitly raised?
  4. Are hardware-detectable errors treated as exceptions that can be handled?
  5. Are there any built-in exceptions?
  6. How can exceptions be disabled?
  7. How and where are exception handlers specified and what is their scope?
  8. How is an exception occurrence bound to an exception handler?
  9. Can information about the exception be passed to the handler?
  10. Where does execution continue,  after an exception handler completes its execution?
  11. Is some form of finalization provided?

Q. What does it mean for an exception to be bound to an exception handler?

A specific exception is to be handled by a specific exception handler also, because different exceptions are to be treated differently.

Q. In a language without exception-handling facilities, we could send an error-handling procedure as a parameter to each procedure that can detect errors that must be handled. What disadvantages are there to this method?

There are several disadvantages of sending error handling subprograms to other subprograms.  One is that it may be necessary to send several error handlers to some subprograms, greatly complicating both the writing and execution of calls.

Another is that there is no method of propagating exceptions, meaning that they must all be handled locally.  This complicates exception handling, because it requires more attention to handling in more places.

Q. Give an example of hardware-detectable execution.

Division by zero.


Q. What is the difference between checked and unchecked exceptions in Java?


Read Ravi Raja Reddy's answer to What is difference between a checked and unchecked exception? on Quora


Q. Why do we use finally block?

  • What happens if an exception you're not handling gets thrown? (I hope you're not catching Throwable...)
  • What happens if you return from inside the try block?
  • What happens if the catch block throws an exception?
A finally block makes sure that however you exit that block, it will get executed. That's important for deterministic cleanup of resources.





Comments

Popular posts from this blog

Difference between abstract class and interface in OOP

Source: Amit Sethi In Interface: > All variables must be public static final. > No constructors. An interface can not be instantiated using the new operator.   > All methods must be public abstract .  

DFS Performance Measurement

Completeness DFS is not complete, to convince yourself consider that our search start expanding the left subtree of the root for so long path (maybe infinite) when different choice near the root could lead to a solution, now suppose that the left subtree of the root has no solution, and it is unbounded, then the search will continue going deep infinitely, in this case , we say that DFS is not complete. Optimality  Consider the scenario that there is more than one goal node, and our search decided to first expand the left subtree of the root where there is a solution at a very deep level of this left subtree , in the same time the right subtree of the root has a solution near the root, here comes the non-optimality of DFS that it is not guaranteed that the first goal to find is the optimal one, so we conclude that DFS is not optimal. Time Complexity Consider a state space that is identical to that of BFS, with branching factor b, and we start the search from th

Difference between a Singly LinkedList and Doubly LinkedList