Solved

Java custom exception class.

Posted on 2010-08-31
4
763 Views
Last Modified: 2013-12-22
I am fairly new to java ( I am a C# programmer).  I am writing a custom error class in Java that extends Exception.  However, I do not see an "inner Exception" in which to hold the original exception that occurred.  When creating a custom Java exception, how do you save the original exception?  

So if I catch an Index out of range exception and wrap that into my custom class, I do not want to loose the index out of range exception.  More importantly I do not want to loose the stack trace.  What is the best practice?
0
Comment
Question by:shanemay
4 Comments
 
LVL 3

Accepted Solution

by:
alanpetersen earned 500 total points
Comment Utility
Your exception should extend from java.lang.Exception, so it already has a Throwable field called 'cause'. You can collect a throwable in the constructor to your exception and store it using super(). Example attached.
package myPackage;

public class CustomException extends Exception {

    public CustomException() {
    }


    public CustomException(String msg) {
        super(msg);
    }
    
    public CustomException(Throwable cause) {
        super(cause);
    }
    
    public CustomException(String msg, Throwable cause) {
        super(msg, cause);
    }
}

Open in new window

0
 
LVL 10

Expert Comment

by:Hegemon
Comment Utility
pass the original exception as the constructor argument of your exception:

... catch (Exception originalException) {
   throw new YourException (originalException);
}
0
 
LVL 3

Expert Comment

by:prakash_parvath
Comment Utility
verify the constructor of your Exception class takes exception as argument and supplies to super class constructor eg: super(exc);

if you didnt overridden the constructor of super class then do so by

YourException(Exception e)
{
//your code
super(e);
}

hope this helps.
0
 

Author Closing Comment

by:shanemay
Comment Utility
Thank you, that is exactly what I needed.  I greatly appreciate your time and consideration on my behalf.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now