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
Solved

Java custom exception class.

Posted on 2010-08-31
4
771 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
runtime exception 2 41
Java Restore security prompts not working 10 56
Tagging and Merging on Branch 1 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

840 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