What is the use of throws keyword, JAVA Programming

Assignment Help:

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.

 


Related Discussions:- What is the use of throws keyword

Url instance and url connection instance, Difference between URL instance a...

Difference between URL instance and URL Connection instance? Ans) A URL instance shows the location of a resource, and a URL Connection instance shows a link for accessing or co

How to catch multiple exceptions explain with example, How to catch multipl...

How to catch multiple exceptions explain with example? If multiple blocks match the exception type, the first block in which matches the kinds of the exception catches it. pub

the file system simulator , • Study and enhance the File System Simulator ...

• Study and enhance the File System Simulator - See the FS_sim slide deck • New features - blockinfo - a new application that displays the information about the blocks in the file

HOW TO MAKE A TRANSCRIPT, Write a java program that computes the scores of ...

Write a java program that computes the scores of 10 students in the following units; 1.Java 2.PHP 3.Python 4.C++ 5.Perl Generate a transcript.

Explain the purpose of the recovery console, Question: a) Give a detai...

Question: a) Give a detailed description of your understanding concerning "Managing User Sessions and Open files". b) What is "Overriding Inheritance" and how can it be do

Midlet for Game, How to create a program using sprite sheet?

How to create a program using sprite sheet?

Develop a custom android app, Develop a Custom Android App Project Descr...

Develop a Custom Android App Project Description: We want you to construct a custom Android app. Once user downloads the app and upon installation the user gets push notif

Explain rmi, RMI is a set of APIs that permits to build distributed applica...

RMI is a set of APIs that permits to build distributed applications. RMI uses interfaces to describe remote objects to turn local method invocations into remote method invocations.

Program for write a class with the header, PART1: Write a class with the...

PART1: Write a class with the header public class OrderedPair > implements Comparable > instance variables private Key x private Key y constructor publicOrd

Life span of variables, While you declare a variable in a function, the var...

While you declare a variable in a function, the variable can be accessed within that function only.  While you exit the function, the variable is destroyed.  These variables are ca

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd