CC 41 C Exception& Threading
CC 41 C Exception& Threading
An exception in Java is a signal that indicates the occurrence of some important or unexpected condition during execution. For example: A requested file cannot be found An array index is out of bounds A network link failed. Java provides an exception handling mechanism for systematically dealing with such error conditions. A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.
Types of exceptions
Sr.no. 1. 2. 3. 4. 5. 6.
IllegalArgumentException Illegal argument used to invoke a method. NegativeArraySizeExcept Array created with a negative size. ion NullPointerException Invalid use of a null reference. IllegalThreadStateExcepti Requested operation not compatible on with current thread state.
7.
Class test { Public static void main(String arg[]){ try{ employee e1=new employee(x,30,2); employee e2=new employee(y,40,4); } Catch (myexception e){ System.out.println(caught + e); }
Single-Threaded Development
Many programs written for class so far have been single-threaded. Thread has a beginning,a body and a end and execute commands sequentially The program begins,run through a sequence of execution and finally ends. At a given pt there is only one statement under execution.
.
WHAT IF V WANT DIFFERENT TASKS OF PROGRAM TO BE EXECUTED CONCURRENTLY
Multithreading
Answer: is java included concept of
multithreading. MULTITHREADING is conceptual programming paradigm where a program is divided into two or more subprograms,which can be implemented at the same time in parellel
Starting a Runnable
Pass an object that implements Runnable to the constructor of a Thread object, then start the thread as before.
Threadtest fr = new Threadtest(); Thread X = new Thread(Threadtest); X.start();
Class threadtest{ public Static void main(String arg[]) { X runnable = new X(); Thread th = new Thread(runnable); th.start(); } }