469,323 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

how to rethrown an exception in java?

2
how to rethrown an exception in java?
Oct 1 '07 #1
8 10576
dmjpro
2,476 2GB
how to rethrown an exception in java?
Post your code and let me know what you tried :-)

Kind regards,
Dmjpro.
Oct 1 '07 #2
Nayna
2
I want to rethrown an exception but I do not know how to rethrown exception so please help me.
Oct 1 '07 #3
r035198x
13,262 8TB
I want to rethrown an exception but I do not know how to rethrown exception so please help me.
Do you know how to catch an exception in first place?
Oct 1 '07 #4
u can use throw new { whatever exception u are going to throw }
Oct 1 '07 #5
JosAH
11,448 Expert 8TB
u can use throw new { whatever exception u are going to throw }
You don't even have to create a new Exception for that; have a look:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    // code here that throws an Exception
  3. }
  4. catch (Throwable t) {
  5.    throw t; // (re)throw it in their face
  6. }
  7.  
kind regards,

Jos
Oct 1 '07 #6
r035198x
13,262 8TB
You don't even have to create a new Exception for that; have a look:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    // code here that throws an Exception
  3. }
  4. catch (Throwable t) {
  5.    throw t; // (re)throw it in their face
  6. }
  7.  
kind regards,

Jos
Spoil sport.
Oct 1 '07 #7
JosAH
11,448 Expert 8TB
Spoil sport.
So sorry; may I lick the legs of your chair? I'll rip my head off now and nail it to the floor;
later I'll rethrow it in the sceptic tank. thank you very much for your merci Sir ;-)

kind regards,

Jos
Oct 1 '07 #8
Sometimes youíll want to rethrow the exception that you just caught, particularly when you use Exception to catch any exception. Since you already have the handle to the current exception, you can simply re-throw that handle:
Expand|Select|Wrap|Line Numbers
  1. catch(Exception e) {
  2.   System.out.println("An exception was thrown");
  3.   throw e;
  4. }
  5.  
Rethrowing an exception causes the exception to go to the exception handlers in the next-higher context. Any further catch clauses for the same try block are still ignored. In addition, everything about the exception object is preserved, so the handler at the higher context that catches the specific exception type can extract all the information from that object.
If you simply re-throw the current exception, the information that you print about that exception in printStackTrace( ) will pertain to the exceptionís origin, not the place where you re-throw it. If you want to install new stack trace information, you can do so by calling fillInStackTrace( ), which returns an exception object that it creates by stuffing the current stack information into the old exception object. Hereís what it looks like:

Expand|Select|Wrap|Line Numbers
  1. //: Rethrowing.java
  2. // Demonstrating fillInStackTrace()
  3.  
  4. public class Rethrowing {
  5.   public static void f() throws Exception {
  6.     System.out.println(
  7.       "originating the exception in f()");
  8.     throw new Exception("thrown from f()");
  9.   }
  10.   public static void g() throws Throwable {
  11.     try {
  12.       f();
  13.     } catch(Exception e) {
  14.       System.out.println(
  15.         "Inside g(), e.printStackTrace()");
  16.       e.printStackTrace();
  17.       throw e; // 17
  18.       // throw e.fillInStackTrace(); // 18
  19.     }
  20.   }
  21.   public static void
  22.   main(String[] args) throws Throwable {
  23.     try {
  24.       g();
  25.     } catch(Exception e) {
  26.       System.out.println(
  27.         "Caught in main, e.printStackTrace()");
  28.       e.printStackTrace();
  29.     }
  30.   }
  31. } ///:~ 
  32. The important line numbers are marked inside of comments. With line 17 un-commented (as shown), the output is:
  33. originating the exception in f()
  34. Inside g(), e.printStackTrace()
  35. java.lang.Exception: thrown from f()
  36.         at Rethrowing.f(Rethrowing.java:8)
  37.         at Rethrowing.g(Rethrowing.java:12)
  38.         at Rethrowing.main(Rethrowing.java:24)
  39. Caught in main, e.printStackTrace()
  40. java.lang.Exception: thrown from f()
  41.         at Rethrowing.f(Rethrowing.java:8)
  42.         at Rethrowing.g(Rethrowing.java:12)
  43.         at Rethrowing.main(Rethrowing.java:24) 
  44. So the exception stack trace always remembers its true point of origin, no matter how many times it gets rethrown.
  45. With line 17 commented and line 18 un-commented, fillInStackTrace( ) is used instead, and the result is:
  46. originating the exception in f()
  47. Inside g(), e.printStackTrace()
  48. java.lang.Exception: thrown from f()
  49.         at Rethrowing.f(Rethrowing.java:8)
  50.         at Rethrowing.g(Rethrowing.java:12)
  51.         at Rethrowing.main(Rethrowing.java:24)
  52. Caught in main, e.printStackTrace()
  53. java.lang.Exception: thrown from f()
  54.         at Rethrowing.g(Rethrowing.java:18)
  55.         at Rethrowing.main(Rethrowing.java:24) 
  56.  
Because of fillInStackTrace( ), line 18 becomes the new point of origin of the exception.
The class Throwable must appear in the exception specification for g( ) and main( ) because fillInStackTrace( ) produces a handle to a Throwable object. Since Throwable is a base class of Exception, itís possible to get an object thatís a Throwable but not an Exception, so the handler for Exception in main( ) might miss it. To make sure everything is in order, the compiler forces an exception specification for Throwable. For example, the exception in the following program is not caught in
Expand|Select|Wrap|Line Numbers
  1. main( ):
  2. //: ThrowOut.java
  3. public class ThrowOut {
  4.   public static void
  5.   main(String[] args) throws Throwable {
  6.     try {
  7.       throw new Throwable(); 
  8.     } catch(Exception e) {
  9.       System.out.println("Caught in main()");
  10.     }
  11.   }
  12. } ///:~
  13.  
Itís also possible to rethrow a different exception from the one you caught. If you do this, you get a similar effect as when you use fillInStackTrace( ): the information about the original site of the exception is lost, and what youíre left with is the information pertaining to the new throw:
Expand|Select|Wrap|Line Numbers
  1. //: RethrowNew.java
  2. // Rethrow a different object from the one that
  3. // was caught
  4.  
  5. public class RethrowNew {
  6.   public static void f() throws Exception {
  7.     System.out.println(
  8.       "originating the exception in f()");
  9.     throw new Exception("thrown from f()");
  10.   }
  11.   public static void main(String[] args) {
  12.     try {
  13.       f();
  14.     } catch(Exception e) {
  15.       System.out.println(
  16.         "Caught in main, e.printStackTrace()");
  17.       e.printStackTrace();
  18.       throw new NullPointerException("from main");
  19.     }
  20.   }
  21. } ///:~ 
  22.  
The output is:

originating the exception in f()
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at RethrowNew.f(RethrowNew.java:8)
at RethrowNew.main(RethrowNew.java:13)
java.lang.NullPointerException: from main
at RethrowNew.main(RethrowNew.java:18)


The final exception knows only that it came from main( ), and not from f( ). Note that Throwable isnít necessary in any of the exception specifications.

You never have to worry about cleaning up the previous exception, or any exceptions for that matter. Theyíre all heap-based objects created with new, so the garbage collector automatically cleans them all up.
Nov 15 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by ChInKPoInt [No MCSD] | last post: by
4 posts views Thread by D'artagnan | last post: by
reply views Thread by star111792 | last post: by
reply views Thread by fergallydon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.