The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with re-throwing exception

2 replies on 1 page. Most recent reply: Apr 18, 2007 11:19 PM by fly man

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
MIrko Panagrosso

Posts: 1
Nickname: microbo
Registered: Apr, 2007

problem with re-throwing exception Posted: Apr 4, 2007 5:58 AM
Reply to this message Reply
Advertisement
Hello,
would you be so gentle to explain us something.
We had a concept problem while studing "thinking in java". We are not able to realize about the rethrowing of a catched exception; particularly we don't understand the following sentence: the Rethrowing an exception causes it to go to the exception handlers in the next-higher context. reading the code we wasn't able to understand who is the next-higher context (number 17):

//: c09:Rethrowing.java
// Demonstrating fillInStackTrace()
import com.bruceeckel.simpletest.*;

public class Rethrowing {
private static Test monitor = new Test();
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void g() throws Throwable {
try {
f();
} catch(Exception e) {
System.err.println("Inside g(),e.printStackTrace()");
e.printStackTrace();
throw e; // 17 HERE THE EXCEPTION IS RE-THROWED BUT WE DON'T UNDERSTAND WHO CATCHES IT
// throw e.fillInStackTrace(); // 18
}
}
public static void
main(String[] args) throws Throwable {
try {
g();
} catch(Exception e) {
System.err.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
monitor.expect(new String[] {
"originating the exception in f()",
"Inside g(),e.printStackTrace()",
"java.lang.Exception: thrown from f()",
"%% \tat Rethrowing.f(.*?)",
"%% \tat Rethrowing.g(.*?)",
"%% \tat Rethrowing.main(.*?)",
"Caught in main, e.printStackTrace()",
"java.lang.Exception: thrown from f()",
"%% \tat Rethrowing.f(.*?)",
"%% \tat Rethrowing.g(.*?)",
"%% \tat Rethrowing.main(.*?)"
});
}
} ///:~

thanks


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: problem with re-throwing exception Posted: Apr 4, 2007 6:30 AM
Reply to this message Reply
> ...we were not able to understand who
> is the next-higher context (number 17):

The 'next higher context' is the method that calls the method that throws the Exception. In your example it is the 'main' method. In other circumstances it could be a method in another class that calls g(). The point is that method g() does not know what method is calling it.

fly man

Posts: 1
Nickname: flyman
Registered: Apr, 2007

Re: problem with re-throwing exception Posted: Apr 18, 2007 11:19 PM
Reply to this message Reply
in my opinion ,the next-higher context is the invoker.


'public static void f() throws Exception'

just do catch the Exception

'throw new Exception("thrown from f()");'

tell the invoker must deal with this Exception

Flat View: This topic has 2 replies on 1 page
Topic: problem with  re-throwing exception Previous Topic   Next Topic Topic: Meebo do

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use