Ticker

6/recent/ticker-posts

Throws Clause in java



Throws clause is used when you know that a method may cause exceptions, but the method does not handle those exceptions. In such a case, a user has to throw those exceptions to the caller of the method by using the throws clause.A throws clause is used when the method is declared.

The following code snippet shows the use of the throws clause:

• return_type is any logical return type according to the values that a method returns.

• para is a list of parameters that needs to be passed in the method.

• throws is the clause used to throw exceptions.

• except1, except2, and except_n are the exceptions that you want to throw.

If want to throw more than one exception, separate them with commas.

 

Exception & its Description below

ArrayIndexOutOfBoundsException:Thrown when you try to access an array with an illegal index.Let's suppose you have declared an array to store a maximum of 5 elements, but while traversing the loop you try to access the 6* element.In such a situation, this exception will be thrown.

ClassNotFoundException:Thrown when you try to access a class, which is not defined in your java program.

FileNotFoundException:Thrown when you try to access a non-existing file or one that fails to open.

I0Exception Thrown when the input-output operation has failed or interrupted.

InterruptedException  Thrown when a thread is interrupted when it is in processing, waiting, or sleeping.

IlligalAccessException Thrown when access to a class is denied.


NoSuchFieldException
Thrown when you try to use any field or variable in a class that does not exist.

 NoSuchMethodException Thrown when you try to access a non-existing method.

 Null Pointer Exception . Thrown when you refer the members of a null object.

 NumberFormatException Thrown when a method is unable to convert a String into a numeric format.

 StringIndexOutofBoundsException Thrown when you try to access a String array with an illegal index.


Thread is the smallest unit of an executable code in a program.It helps you divide a process in 1 multiple parts to speed up the process.A process is a program that executes in the memory as a single thread.Programs that run as single thread can cause problems when you perform two or more tasks simultaneously.

For example, while playing an online game, we, sometimes, come across a situation where the game does not show updated scores but displays graphics properly, that is a situation when the game is properly running but without updating the score.Here, displaying graphics and updated scores are two different tasks to be handled simultaneously.Therefore, it is important to ensure that the game should provide support to handle these multiple tasks to make the game fag and interactive enough to play.

To overcome such problems, Java provides built-in support for multi thread programming, which enables a single program to perform multiple tasks simultaneously.In multithreaded programming, each thread is assigned a task to perform and each of them executes independently in a program.Multiple threads within a program share the same memory address space, and therefore they share the same set of variables and objects among themselves.

Threads are used to implement concurrency in Java programs.Concurrency is the simultaneous execution of multiple tasks.These multiple tasks can be implemented either as separate processes, or as a set of threads within a single program.For example, working in a word processor while listening an audio Ale on the same computer are two tasks that are implemented as two separate processes. However, we often need a single program or process to perform multiple tasks at the same time, such as a word processor that can check spellings of words in Here, the word processor is a single program that performs two tasks simultaneously —checking the spelling, and writing a document.These two tasks are implemented as two separate threads within a word processor.Threads are light-weight processes, because creating a new thread requires fewer resources than creating a new process.For example, a newly created thread shares the same address space with other threads in a program, whereas every individual process has its own separate address space.

Therefore, creating a thread involves less overhead than creating a process.The simultaneous execution of threads is implemented with the help of 'time-slice'. The 'time-slice' is the processing time that each thread gets for its processing.Each of these threads executes according to the time slice allotted to them but it appears to you as if these threads were running at the same time.The objective of multi-threading is to utilize the idle time of CPU.

 

For more understanding you can also read below mentioned topic from online:

*.python programming app development  

Post a Comment

0 Comments