?
Solved

Java custom exception class.

Posted on 2010-08-31
4
Medium Priority
?
774 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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