Inner Exceptions 

J# exception classes provide constructors that allow the original exception to be saved when a new exception is thrown in place of another exception.

java.lang.Throwable(String message, System.Exception innerException);
java.lang.Exception(String message, System.Exception innerException);
java.lang.Error(String message, System.Exception innerException);
java.lang.RuntimeException(String message, System.Exception innerException);

Parameters

  • message
    Text describing the nature of the problem for use in error messages.
  • innerException
    The original .NET Framework exception preserved when rethrowing a .NET Framework exception as a Java exception.

Remarks

In the .NET Framework, System.Exception is the root of the exception hierarchy. This exception type supports the concept of inner exceptions. Inner exceptions are useful when there is a need to handle an exception in a catch block and throw a new exception. If the callers up the stack that catch the new exception are interested in knowing why the exception was thrown, they may get the original exception by querying the InnerException property. Visual J# supplies additional overloads of the exception constructors that take an additional parameter, innerException, which is the original exception. The inner exception can be obtained by calling get_InnerException().

Example

// innerexcept1.jsl
// This example shows a class that wraps a FileNotFoundException in its 
// own exception class. The original exception can be recovered using 
// the get_InnerException method on the System.Exception class.
import System.*;
import java.io.*;
import System.Collections.*;

class ConstructionException extends System.Exception
{
    public ConstructionException(String message, System.Exception innerException)
    {
        super(message, innerException);
    }
}

class DataStructure
{
    ArrayList m_List;
    public DataStructure() 
    {
        m_List = new ArrayList();
    }

    public void Add(int c)
    {
            m_List.Add((System.Object)(Int32)c);
    }


    public static DataStructure ConstructFromFile(String filename) throws ConstructionException
    {
        DataStructure ds = new DataStructure();
        try 
        {
            int c;
            // Could throw FileNotFoundException.
            FileReader reader = new FileReader(filename);

            // Could throw IOException.
            while ((c = reader.read()) != -1)
            {
                ds.Add(c);  
            }
            
        } 
        catch (IOException e)
        {
            Console.WriteLine( e.get_Message() );
            throw new ConstructionException("Error creating the data structure.", e);
        }
        return ds;
    }
}


class CMain
{
    public static void main()
    {
      try
        {
            // Assume myfile.dat doesn't exist.
            DataStructure.ConstructFromFile("myfile.dat");
            
        }
        catch(System.Exception e)
        {
        Console.WriteLine("Exception occurred: " + e.get_Message());

            System.Exception innerEx = e.get_InnerException();
            if (innerEx != null)
            Console.WriteLine("Cause: " + innerEx.get_Message() ); 
        }
    }
}

This example shows how to extract the inner exception in a catch clause that catches java.lang.Throwable. Because java.lang.Throwable is wrapped in an extra exception layer in order to support catching all exceptions, it is necessary to invoke get_InnerException twice. For more information, see Visual J# Exception Hierarchies.

// innerexcept2.jsl

import System.*;
import java.io.*;
import System.Collections.*;

class ConstructionException extends System.Exception
{
    public ConstructionException(String message, System.Exception innerException)
    {
        super(message, innerException);
    }
}

class DataStructure
{
    ArrayList m_List;
    public DataStructure() 
    {
        m_List = new ArrayList();
    }

    public void Add(int c)
    {
        m_List.Add((System.Object)(Int32)c);
    }


    public static DataStructure ConstructFromFile(String filename) throws ConstructionException
    {
        DataStructure ds = new DataStructure();
        try 
        {
            int c;
            // Could throw FileNotFoundException.
            FileReader reader = new FileReader(filename);

            // Could throw IOException.
            while ((c = reader.read()) != -1)
            {
                ds.Add(c);  
            }
            
        } 
        catch (IOException e)
        {
            Console.WriteLine( e.get_Message() );
            throw new ConstructionException("Error creating the data structure.", e);
        }
        return ds;
    }
}


class CMain
{
    public static void main()
    {
        try
        {
            // Assume myfile.dat does not exist.
            DataStructure.ConstructFromFile("myfile.dat");
            
        }
        catch(java.lang.Throwable t)
        {
            // Get the exception out of the Throwable wrapper.
            System.Exception e = ((System.Exception) t).get_InnerException();
        Console.WriteLine("Exception occurred: " + e.get_Message());

        // Get the true inner exception.
        System.Exception innerEx = e.get_InnerException();
            if (innerEx != null)
            Console.WriteLine("Cause: " + innerEx.get_Message() ); 
        }
    }
}

Sample Output

Could not find file 'file-name'.
Exception occurred: Error creating the data structure.
Cause: Could not find file 'file-name'.

Sample Output

Could not find file 'file-name'.
Exception occurred: Error creating the data structure.
Cause: Could not find file 'file-name'.