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

How many bits are used to show unicode and ascii, How many bits are used to...

How many bits are used to show Unicode, ASCII, UTF-16, and UTF-8 characters? Unicode requires 16 bits and ASCII require 7 bits although the ASCII character set uses only 7 bits

Write a parser, I have a parser. it is written in Java. I need to add a has...

I have a parser. it is written in Java. I need to add a hash table to it. I am wondering about if you can do it.

Queues, we can insert elements at rear and remove at front bt my question i...

we can insert elements at rear and remove at front bt my question is that how we insert at front in circular queue

Distributed ticket reservation system, In this programming assignment, you ...

In this programming assignment, you are going to implement the simple distributed ticket reservation system (DTRS) from Assignment 1 in CORBA using Java IDL. In addition to the thr

Mine craft plug-in coder, Mine craft plug-in Coder Project Description: ...

Mine craft plug-in Coder Project Description: Some custom plugging for my Mine craft server. Skills required: Java, Editing

Program to create 5 objects and get them interact together, In this assignm...

In this assignment, you will create 5 objects and get them to interact together. You will create theatres for which patrons will buy tickets from a box office to watch movies.

What does the keyword "synchronize" mean in java, What does the keyword " s...

What does the keyword " synchronize " mean in java. What are the usage and disadvantages of synchronization?

How dbms is better to use than flat-file database, How can you justify that...

How can you justify that a DBMS is better to use than Flat-file database? A DBMS takes care of the storage, retrieval, and management of large data sets on a Database. It give

Query and update the text database for a library , Implement a program to q...

Implement a program to query and update the text database for a library using a client/server model allowing multiple clients safe access to the database, and using a producer-cons

Jumps in loops, explain jumps in loops and explain labelled loops

explain jumps in loops and explain labelled loops

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