public class Tigers { public static void main( String args[] ) { try { petfood1(
ID: 3548521 • Letter: P
Question
public class Tigers {
public static void main( String args[] )
{
try { petfood1(); }
catch ( CatException e ) {
System.err.println( e.getMessage() + " The stack trace is:" );
e.printStackTrace();
}
}
public static void petfood1() throws Exception {
petfood2();
}
public static void petfood2() throws Exception {
petfood3();
}
public static void petfood3() throws Throwable {
throw new CatException();
}
}
class CatException extends Exception {
public CatException() {
super( "user defined exception." );
}
}
Explanation / Answer
Hi,
Please find below the answer without Copilation.
Have caught throwable in Petfood 2 function and caught exception in main function.
public class Tigers {
public static void main(String args[]) {
try {
petfood1();
} catch (CatException e) {
System.err.println(e.getMessage() + " The stack trace is:");
e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getMessage() + " The stack trace is:");
e.printStackTrace();
}
}
public static void petfood1() throws Exception {
petfood2();
}
public static void petfood2() throws Exception {
try {
petfood3();
} catch (Throwable e) {
throw new Exception();
}
}
public static void petfood3() throws Throwable {
throw new CatException();
}
}
class CatException extends Exception {
public CatException() {
super("user defined exception.");
}
}
Hope it helps.
Thanks,
Sri
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.