Which two method declarations are valid options to replace /* method declaration */?

Given the code fragment:
<code>
/* method declaration */ {
try {
String className = “java.lang.String”;
String fieldname = “somefield”;
Class c = Class.forName(className);
Field f = c.getField(fieldname);
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}
</code>

Which two method declarations are valid options to replace /* method declaration */?

Given the code fragment:
<code>
/* method declaration */ {
try {
String className = “java.lang.String”;
String fieldname = “somefield”;
Class c = Class.forName(className);
Field f = c.getField(fieldname);
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}
</code>

Which two method declarations are valid options to replace /* method declaration */?

A.
public void getMetadata ()

B.
public void getMetadat ()

C.
public void getMetadata () throws Exception

D.
public void getMetadata () throws NoSuchFieldException

E.
public void getMetadata () throws classNotFoundException

F.
public void getMetadata () throws ClassNotFoundException, NoSuchFieldException.

Explanation:
We must specify that the getMetaData method can throw both ClassNotFoundException (line Class c = Class.forName(className);) and a NoSuchFieldException (line Field f = c.getField(fieldname);).
We can do this by either declare that all exception can be thrown or that these two specific exceptions can be thrown

Note: Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following:
* A try statement that catches the exception. The try must provide a handler for the exception.
* A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception.
Code that fails to honor the Catch or Specify Requirement will not compile.
Reference: The Java Tutorials, The Catch or Specify Requirement



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Vins

Vins

C, F . See Explanation