Home > Software > Introduction to Java > Object Oriented Concepts

Satellite Phones - Contents Functioning of a Satellite Phones

e. Exception handling

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Advantages of exception handling:

        1.      It is separates error handling code from “regular” code

        2.      Propagating errors up the call stack (without tedious programming)

        3.      Grouping error types and error differentiation

 

Categories of exceptions:

1.     Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. These are usually invalid conditions in area, which is not in reach of programmer. These exceptions cannot simply be ignored at the time of compilation. Some examples are missing files or database problems.

2.     Runtime exceptions: A runtime exception is the programmer could have avoided an exception that occurs that probably. These reflect errors in programming logic.

3.     Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

 

Java uses try and catch keywords for exception handling. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try
{
   //Protected code
}
catch (ExceptionName e1)
{
  //Catch block
}

An exception causes a jump to the end of try block. If the exception occurred in a method called from a try block, the called method is abandoned. If there’s a catch block for the occurred exception or a parent class of the exception, the exception is now considered handled. At least one ‘catch’ block or one ‘finally’ block must accompany a ‘try’ statement. If all 3 blocks are present, the order is important. (try/catch/finally) finally and catch can come only with try, they cannot appear on their own.

Regardless of whether or not an exception occurred or whether or not it was handled, if there is a finally block, it’ll be executed always. (Even if there is a return statement in try block).

If there was no exception or the exception was handled, execution continues at the statement after the try/catch/finally blocks.  

   

3. Object oriented Concepts

a. Classes and objects

1.   Classes: A class is a structure that defines data and functions to operate/modify/work on that data.

2. Objects :Object is a feature of a class, which is used for working on the particular properties of the class. A class can have any number of objects in memory at one time

b. Scope

1.      Names are used to access methods and variables.

2.      Different namespaces are used in java some of them are given below:

i.       Package names

ii.       Type names

iii.     Variable names

iv.    Method names

v.      Local variable names (including parameters)

vi.    Labels

vii.   Each declared Enums has its own namespace.

 

3.      The scope of a name is the part of a class in which it can be seen.

Variable scope

 

Owner

Declared in

Life span

Can be used

Local variable

Method where declaration is done

Inside method

Till method executes

Only inside method where declaration is done

Instance

Each object has a copy

Inside a method, outside a class without static

As long as object exists

In all instance methods of the class

Class

Class where declaration is done

Inside a method, outside a class with static

As long as containing class is loaded

In all methods of class

 

Method scope

The scope of a method declared anywhere in a class is the entire class.  

The scope of a method's formal parameters is the entire method.

 
Satellite Phones - Contents Functioning of a Satellite Phones
     Home                                                Copyright (C) 2003-2012 TutorialsWeb.com                                   Disclaimer                                           Sitemap