OtherPapers.com - Other Term Papers and Free Essays
Search

Exception - an Abnormal Event in Program

Essay by   •  June 12, 2011  •  Essay  •  505 Words (3 Pages)  •  1,790 Views

Essay Preview: Exception - an Abnormal Event in Program

Report this essay
Page 1 of 3

Introduction

An exception is an abnormal event that is likely to happen when a program is executing. For instance:

(a) The computer could run out of memory. Not usually, but there is a possibility that it can happen.

(b) Calling the parseInt( arg ) method with argument, arg, that cannot be parsed into an integer.

(c) Dividing an integer value by zero.

(d) Using an index that is outside the range of an array.

(e) Accessing an ArrayList or a Vector that is empty.

(f) Attempting to access a file that does not exists.

(g) Attempting to access a file that is empty.

Exception must be distinguished from other types of programming errors. Programming errors can be categorized into three types - syntax errors, logic errors, and runtime errors. Errors that occur during compilation are called syntax errors or compilation errors. These errors result from the lack of compliance with the rules of the programming language. Logic errors on the other hand occur when the program produces unintended results. Runtime errors occur when the program attempts to perform tasks that are impossible. Against this background the program terminates abnormally. If we analyze the examples above, we will see that those tasks are impossible to done, hence, they are all runtime errors.

The concept of exception is a customary way in Java to indicate that an abnormal condition has occurred. When a method encounters an abnormal condition that it cannot handle itself, it may throw an exception. Throwing an exception is like tossing a ball out of a window hoping that there is someone outside there who will catch it.

The central mission of exception handling is to transfer control from where the error occurred to a section of the program that can deal with the exception. The section of code that deals with the exception is called an exception handler. When you program in Java, you must position exception handlers strategically, so that your program will catch and handle all exceptions that are likely to be thrown.

Response to an Exception

A program can be made to:

(a) Ignore exceptions, in which case the program would abort, or produce incorrect result.

(b) The user could be allowed to fix the problem, but usually the user of the program is not a programmer.

(c) The programmer could design the program in such a way that if and when an exception occurs, program control would pass the exception to an appropriate block of codes within the program where the exception can be taken care of.

...

...

Download as:   txt (3.1 Kb)   pdf (59.7 Kb)   docx (9.7 Kb)  
Continue for 2 more pages »
Only available on OtherPapers.com