• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catch me if You Can  RSS feed

 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My error class is defined like this:
///---------------------------------------------------------------
//------------------------------------------------------------------

In other class I try and catch as follows, and in the catch block, I want to throw new. How do I "throw new"?


I want to get "why" information and also "trace" information.
How do I extract and pass that info. to initSomething() caller?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what you're asking, the solution is to pass the original exception into the new exception's constructor. Java's Exception class has had this ability since JDK 1.4 with the constructor public Exception(String message, Throwable cause). The printStackTrace() method is also aware of "causes" now and will include them in the stack trace. The only problem is whether your Corba exception base class provides such a constructor. If not, you'll have to get a bit tricky with overrides.

Let me know if this helps.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that exception doesn't have a constructor which accepts another exception as a cause. But I don't think that any trickery would be required, because there's the "initCause(Throwable)" method which was also added in 1.4. So when you create the exception, you then call that method to set up the cause with the real exception which caused you to throw that one.
 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Greg, Paul.

Greg, I tried
throw new NotExist(e.getMessage(), e.getCause());
but it gave me error, constructor (string, throwable) not defined

Paul,
How would I put that into code?
The other way of saying this is that you will have to come down to my level.



 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell you because I can't understand what you are trying to do. You have code in a catch block which catches this "NotExist" exception, and then what? You want to throw another NotExist exception? Why can't you just throw the one you already have? (And if you can, then why did you catch it in the first place?)

Or if you want to throw a new exception, what's going to go into that description?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!