Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java custom exception class.

Posted on 2010-08-31
4
Medium Priority
?
777 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 2000 total points
ID: 33571403
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
ID: 33571747
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
ID: 33574535
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
ID: 33576415
Thank you, that is exactly what I needed.  I greatly appreciate your time and consideration on my behalf.  
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 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