Question about try and catch methods I would like to know how the following prog
ID: 3683149 • Letter: Q
Question
Question about try and catch methods
I would like to know how the following program outputted the following code at the bottom. I'm trying to follow it but I don't understand
This is the question, and the output is at the bottom--
////////////////////////////////////////////////////////////////////////////
What is the output from the following program?
Note: if an exception is not handled, then the program is terminated immediately.
class Test {
static void f1()
{ throw new RuntimeException("hello"); }
static void f2()
{
try
{ f1(); }
catch (Exception e)
{ System.out.println("FFF"); }
finally
{ System.out.println("GGG"); }
f1();
System.out.println("HHH");
}
public static void main(String[] argv)
{
try
{ f2();
f1(); }
catch (Exception e)
{ System.out.println("AAA"); }
try
{
f1(); }
catch (Exception e)
{ System.out.println("BBB"); }
finally
{ System.out.println("CCC"); }
System.out.println("DDD");
f2();
System.out.println("EEE");
f1();
}} // END class Test
///////////////////////////////////////////////////
//Output
FFF // main: call f2()
GGG
AAA
BBB // main: call f1()
CCC
DDD
FFF // main: call f2()
GGG
Exception!
Explanation / Answer
//please read comments...in number wise... to understand flow...
//we use try{}catch{}final for handling exceptions...
//first try{} executed//if any exception found handled in catch..and after final will executed..
class Test {
static void f1()
{ throw new RuntimeException("hello"); }//3. function f1() throws runtime exception "hello"
static void f2()
{
try
{ f1(); }//2. it calls function f1() which throws the runtime exception....Hello
catch (Exception e)//3. Runtime exception thrown by function f1() is handled here...
{ System.out.println("FFF"); }//4. FFF get printed...
finally//5. this will executed next...
{ System.out.println("GGG"); }//6. GGG get printed...
f1();//7. it calls function f1() which throws the runtime exception....Hello... but it is not handled...
// to solve this put f1() in try{}catch{} to handle the exception...
System.out.println("HHH");//8. the exception thrown by above f1() is not handled...so this line is not executed.. it moves to main()
}
public static void main(String[] argv)
{
try
{ f2();//1. first function f2() is called... it moves to function f2()
f1(); }//9.it calls function f1() which throws the runtime exception....Hello
catch (Exception e)//10. Runtime exception thrown by function f1() is handled here...
{ System.out.println("AAA"); }//11. AAA get printed...
try
{
f1(); }//12. it calls function f1() which throws the runtime exception....Hello
catch (Exception e)//13. Runtime exception thrown by function f1() is handled here...
{ System.out.println("BBB"); }//14. BBB get printed...
finally
{ System.out.println("CCC"); }//15. CCC get printed ...
System.out.println("DDD");//16. DDD get printed....
f2();//17. Again function f2() is called....again read comments..2,3,4,5,6,7,8...
System.out.println("EEE");//18. EEE get printed.....
f1();//19. it calls function f1() which throws the runtime exception....Hello
//the runtime exception is not handled... here. and in function f2()
//so you have got an exception at the end////
// to solve this put f1() in try{}catch{} to handle the exception...
}} // END class Test
//if this answer is satisfactory do comment...
//if you have any doubts feel free to ask me...
output:-
FFF
GGG
AAA
BBB
CCC
DDD
FFF
GGG
Exception!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.