Solved

Java custom exception class.

Posted on 2010-08-31
4
773 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
alanpetersen earned 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

705 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