Saturday, May 30, 2009

[JAVA] Exception Handling

Exception Handling :- The purpose of exception handling mechanism is to detect and report that an exception is occurred so that appropriate action can be taken. There are four phase in entire exception handling process –

1) Find the problem (Hit the exception)

2) Inform that an error has occurred (Throw the exception)

3) Receive the error information (Catch the exception)

4) Take necessary actions (Handle the exception)


Syntax of Exception Handling -
Try

{

Statement; //generates an exception

}

Catch

{

Statement; //process the exception

}



Example –

Class SimpleDivision(){

Private static void Main(){

int a=2, b=0, c=6;

try{

c=a/b;

system.out.println(c);

system.out.println(“Successful.”);

}

Catch(Arithmatic Exception e){

system.out.println(“Not successful.”);

}

}

}


[ OUTPUT : Not Successful. ]

[JAVA] Dead Lock

Dead Lock :- A special type of error that may occur in multithreading Java programming is Dead Lock. When two threads are accessing two synchronized methods such that t1 and t2 are using synchronized method x and y. If the thread in x try to invoke the synchronized method y and on the other hand the thread in y try to invoke the synchronized method x, no one will be successful, they will wait forever. In this situation the whole process is entries to a cycle. This is called Dead Lock.

Friday, May 29, 2009

[JAVA] Synchronization

Synchronization :- In Multithreading there may a situation occur that one thread is trying to read a record from a file while another is still updating the same file. At that time we may face serious problems. Java enables us to overcome this problem by using a technique known as Synchronization.

[JAVA] Multithreading

Multithreading :- Multithreading is programming concept like multitasking in OS. Multithreading allows executing multiple tasks as methods in the program simultaneously.