28
Exception Handler Exception Handler

Exception handler

  • Upload
    dishni

  • View
    1.692

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Exception handler

Exception Handler Exception Handler

Page 2: Exception handler

Questions # 1Questions # 1

Define Exception, exception handler, raising an exception, disabling an exception, continuation and built in exception.

Page 3: Exception handler

AnswerAnswer

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.

Page 4: Exception handler

AnswerAnswer

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.

Raising an Exception An object raises an exception when it has trapped an error. An object raises an exception by sending itself the "raiseException" message, passing an error number as a parameter.

Page 5: Exception handler

AnswerAnswer

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.

Built in Exception is an exception that is constructed as part of the language and is not detachable.

Page 6: Exception handler

Question # 2Question # 2

What are the design issues for an exception handling?

Page 7: Exception handler

AnswerAnswer

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.

Page 8: Exception handler

AnswerAnswer

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.

Page 9: Exception handler

AnswerAnswer

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.

Page 10: Exception handler

AnswerAnswer

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.

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.

Page 11: Exception handler

AnswerAnswer

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.

7. Whether exception, either built in or user-defined, can be temporarily or permanently disabled.

Page 12: Exception handler

Question # 3Question # 3

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

Page 13: Exception handler

AnswerAnswer

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)

Page 14: Exception handler

AnswerAnswer

– Task - no propagation; if it haandler, execute it; in either case, mark it "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)

Page 15: Exception handler

Question # 4Question # 4

What is the parent class of most Java user-defined exception classes?

Page 16: Exception handler

AnswerAnswer

Throwable class. Based on the book, All Java exceptions are objects of classes that are descendants of the Throwable class

Page 17: Exception handler

Question # 5Question # 5

How can an exception handler be written in Java so that it handles any exceptions?

Page 18: Exception handler

AnswerAnswer

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){…}

Page 19: Exception handler

Question # 6Question # 6

What is the difference between a C++ throw specifications and a Java throws clause?

Page 20: Exception handler

AnswerAnswer

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.

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.

Page 21: Exception handler

AnswerAnswer

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.

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.

Page 22: Exception handler

Question # 7Question # 7

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

Page 23: Exception handler

AnswerAnswer

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.

Page 24: Exception handler

Question # 8Question # 8

Can you disable a Java exception?

Page 25: Exception handler

AnswerAnswer

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.

Page 26: Exception handler

Question # 9Question # 9

What is the purpose of the Java finally clause?

Page 27: Exception handler

AnswerAnswer

Purpose: To specify code that is to be executed, regardless of what happens in the try construct

Page 28: Exception handler

Kristine AnchetaKristine Ancheta Stephanie Rosanna BlanzaStephanie Rosanna Blanza Desney Ann CaracotDesney Ann Caracot Roshe Nikhka de GuzmanRoshe Nikhka de Guzman Rhys Anton GabionRhys Anton Gabion Erick Lester SumugatErick Lester Sumugat Carlo MarCarlo Mar