Answer to Question #227535 in Java | JSP | JSF for umme habiba sumi

Question #227535

 Creating a user defined Exception “FileFormatException” by extending IOException as following way

class FileFormatException extends IOException {

public FileFormatException ( ) { }

public FileFormatException ( String gripe ){

super (gripe);

}

And using that custom exception as follows:

String readData( BufferReader in ) throws FileFormatException {

……. 

while ( ……) { 

if ( ch = = -1) // EOF encountered 

{

if ( n < len )

throw new FileFormatException ( ) ; 

}

………

}

return s ;

i) What are the differences between throw and throws? ii) Analyze the above code and write down your comments on whether the procedure is correct ways to use exception or not.


1
Expert's answer
2021-08-20T14:17:29-0400

i) Differences between throws and throw.

  • Throws clause is applied when declaring exceptions. This means that throws clause functions similarly as a try-catch block while throw is useful when throwing an exception explicitly.
  • Exception class names always follow the throws clause while while an object of Exception class follows the keyword throw.
  • Throws clause is written in the method signature while throw keyword is placed within a method body.

ii) The use of exception in the above code is correct because of the following reasons:

  •  FileFormatException follows the keyword throws, indicating exception declaration.
  • new FileFormatException ( ), a instance of FileFormatException class follows the keyword throw.
  • The throw keyword is also used inside the body of a function

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS