What is the use of throws keyword, JAVA Programming

What is the use of throws keyword ?

Rather than explicitly catching an exception you can declare in which your method throws the exception. This passes the repsonsibility to handle it to the method in which invokes your method. This is done with the throws keyword. For instance,
public static void copy(InputStream in, OutputStream out)
throws IOException {

byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);

A single method may have the potential to throw more than one kind of exception. In this case the exception clases are just separated through commas. For example,
public BigDecimal divide(BigDecimal value, int roundingMode) throws ArithmeticException, IllegalArgumentException

You can declare in which your method throws runtime exceptions though you do not have to. The major use of this is as documentation for the programmer. It can also be useful in white box testing.


Posted Date: 5/16/2013 2:59:23 AM | Location : United States

Related Discussions:- What is the use of throws keyword, Assignment Help, Ask Question on What is the use of throws keyword, Get Answer, Expert's Help, What is the use of throws keyword Discussions

Write discussion on What is the use of throws keyword
Your posts are moderated
Related Questions
Illustrate the parser method The below table summarizes the parser methods available to a java programmer.

What are the advantages of multimedia presentations? Write any five. Multimedia presentation is a excellent tool for effective communication: Advantages: 1. Easy to make l

public class Foothill {    public static void main(String[] args)    {       Client person_1 = new Client("Bruce Ruprecht", 16000, 10);       Writer writer_1 = new Writer("Adam Buf

Explain The different kinds of exceptions ? Checked Exceptions  : Environmental error in which cannot necessarily be detected through testing; e.g. broken socket, disk full,

I need integrate template to Java system Project Description: We have around 60 files for a java backend and want to implement a template, the system is complete it requires

What is Event Handler works? An event handler is a command which is used to call a function when an event happens, such as the user clicking a button or mouse.

You must design, code and demonstrate a J2ME program (a program capable of running on mobile telephones) according to the specification given in the next section.  The overall cour

How can we include images in a web page using HTML and Java script?2011 Images in HTML It is quite straight forward to include gif and jpg images in an HTML webpage using the

The JMS provider handles security of the messages, data conversion and the client triggering. The JMS provider verifies the level of encryption and the security level of the messag

How does JVm do dynamic checking The JVM also does "dynamic" checking at runtime for certain operations, like pointer and array access, to make sure they are touching only memo