Given the code fragment: What is the result?
A.
Compilation fails at both line n2 and line n3.
B.
Reading Card Checking Card
C.
Compilation fails only at line n1.
D.
Compilation fails only at line n2.
E.
Compilation fails only at line n3.
Given the code fragment: What is the result?
A.
Compilation fails at both line n2 and line n3.
B.
Reading Card Checking Card
C.
Compilation fails only at line n1.
D.
Compilation fails only at line n2.
E.
Compilation fails only at line n3.
D.
Compilation fails only at line n2.
Answer is Option E.
Error:(18, 20) java: unreported exception java.lang.Exception; must be caught or declared to be thrown
Exceptions must either handled or declared.
Answer is option E
D salta solo en la primera ocurrencia
D.
Compilation fails only at line n2.
The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor’s throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
It is
E.
Compilation fails only at line n3.
Because ex.readCard() throws exception that must be handled
Answer: E
Tested
e
Answer: E
When running in the compiler i get the following output.
public class Test {
void checkCard(int cardNo) throws Exception {
System.out.println(“Reading Card”);
}
void readCard(int cardNo) throws RuntimeException {
System.out.println(“Checking Card”);
}
public static void main(String[] args) throws Exception {
Test ex = new Test();
int cardNo = 12344;
ex.checkCard(cardNo);
ex.readCard(cardNo);
}
}
Reading Card
Checking Card
Compiles fine
Sorry. i shouldn’t have added the “throws Exception” to the main method.
When removing that a Compile error at line /n2
actually, line /n3 “Unhandled exception type Exception”.
IF the main method would also have “throws Exception” then compiles fine.
Decide man! ;)))
RE are thrown by the JVM.
Only the checked ones must be handled at the compile time.