ERROR HANDLING
There are three types of errors that have to be handled for making a successful program.
- Syntax Error or Compile-time Error
- Logical Error
- Run-time Error or Exception
Syntax errors or compile time errors occur at the time of compiling the program and they are resolved by following the correct syntax of programming.
For example: int age = 21 /// in this code the semi-colon (terminator operator) is missing so in order to rectify this error the user has to put the semi-colon at the end of the line thus following the correct rule of syntax.
Logical errors are raised when the program does not gives the desired output for which the program was created.
For example : A program was made to print the table of 2 up-to 10 times but the table is getting printed 11 times or 9 times which means that there is some mistake in the logic of the program though the program running and all syntax are correct.
Run-time Exceptions are raised by the interaction of the user with the output of the program.
For example: There is a program which prints the result of a division in which denominator can not be zero but the user can input zero during the execution of the program hence the result of division by zero is infinity which can not be stored or displayed in the program so the program will terminated abruptly causing the failure of the project.
If this exception could have been handled somehow then rest of the code of the program would have executed. So, to implement this the try, catch & finally block were introduced in the programming.
try block: The code in your program which has possibility of raising a run-time exception should be placed in try block.
catch block: The code which you want to execute immediately after the rise of a run-time exception should be placed in catch block.
finally block: The code which you want to execute irrespective of whether any run-time exception has raised or not should be placed in finally block.
its very useful
ReplyDelete