An exception (or exceptional event) is a problem that arises during the execution of a program.

An Exception can occur in any scenarios like

  1. while processing invalid data
  2. while communicating to external resources
  3. Server related issues
  4. Memory can reach max level.

In any of the scenario if the application or program flow terminates while executing any scenario, application said to be unstable. Application must remain stable while handling any invalid scenario as well, handling the exception or event thats disturbs the normal flow.

Example: Assume a scenario,

A operation written return such a way it accepts age for some reason.

String processAge ( String age ) {

—-
int age = Integer.parseInt (age );
—-

return ” Processed age is : ” + age);

}

In  normal scenario,

Ex 1: processAge(“18”);

Returns :  Processed age is : 18

In  exception scenario,

age value passed as “EIGHTEEN” instead of 18.

Ex 1: processAge(“18”);

Program doesnot return instead terminates from the flow after this line

int age = Integer.parseInt (age ); // it will be parsing EIGHTEEN string literal.

 

Closely look into the following flow,

During the execution statement 1 to 10 must flow safely till the end last line of flow. If the flow said to be success and safe.

Fail Scenario or unsafe flow:

Here exception is not handled in the following flow ,

While executing in statement 5, causes exception  then following statements from statement 6 – 10 will not be executed and it terminates.

{

       // Start of Program Flows

statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;

// End of Program Flow

}

Handle the exception for the statement 5; then statement 6 – 10 will flow safely and program will not terminate abruptly

           So Now How to Handle Exception

Advertisements