PRB: Compilation Errors When You Try to Catch a Common Language Runtime Exception Using Exception Class (327770)



The information in this article applies to:

  • Microsoft Visual J# .NET (2002)
  • Microsoft Visual J# .NET (2003)

This article was previously published under Q327770

SYMPTOMS

When you try to catch a common language runtime exception by using the Exception class in your Visual J# .NET application, you may receive the following error messages during compilation:
C:\MyJSharpConsoleApplication\Class1.jsl(1): Type 'Exception' is imported by System and java.lang. Using the java.lang type
-or-
C:\MyJSharpConsoleApplication\Class1.jsl(10): Exception 'System.Exception' is not caught and does not appear in throws clause

CAUSE

When the namespace is not qualified before the Exception class, the Visual J# .NET compiler considers Exception as java.lang.Exception instead of as System.Exception in the catch block. With the Visual J# .NET compiler, you cannot catch a common language runtime exception (that is, System.Exception) by using java.lang.Exception, and you receive the compilation error messages.

RESOLUTION

To resolve this problem, catch the common language runtime exception by using System.Exception instead of by using Exception. The following sample code shows how to catch a common language runtime exception by using System.Exception:
package MyJSharpConsoleApplication;
import System.*;

public class Class1
{
   public static void main(String[] args)
   {
      try
      {
         throw new System.Exception("New CLR Exception");
      }
      catch(System.Exception e)
      {
         System.Console.WriteLine(e.ToString());
      }
      finally
      {
         System.Console.Read();
      }
   }
}

STATUS

This behavior is by design.

MORE INFORMATION

When an error occurs, either the system or the currently executing application reports the error by generating an exception that contains information about the error. Just as you can catch the exception with the Java language, you can catch the exception specific to the Visual J# .NET language by using the java.lang.Exception class.

Some of the Java language exceptions have equivalents in the Microsoft .NET Framework. When you catch a Java language runtime exception in your source code, the Visual J# .NET compiler ensures that the resulting executable code also catches its .NET Framework equivalent. When you develop applications, you can concentrate on Java-specific exceptions and can typically ignore the .NET Framework exceptions that have Java language equivalents. However, to catch an exception that does not have an equivalent .NET Framework exception, you must use System.Exception. Java.lang.Exception derives from the java.lang.Throwable class. The java.lang.Throwable class in turn derives from System.Exception. Therefore, Visual J# .NET uses System.Exception instead of catching Throwable for all exceptions as the Java language does. In the .NET Framework, System.Exception is the base class for all exceptions. Therefore, to catch all the common language runtime exceptions, you use System.Exception.

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET.
  2. Create a new Visual J# .NET console application named MyJSharpConsoleApplication.
  3. Replace the existing code with the following code:
    package MyJSharpConsoleApplication;
    import System.*;
    
    public class Class1
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			throw new System.Exception("New CLR Exception.");
    		}
    		catch(Exception e)
    		{
    			System.out.println("Attempting to catch CLR Exception using Exception : " + e.ToString());
    		}
    		finally
    		{
    			System.out.println("This message is printed always!!!");
    		}
    	}
    }
    
  4. On the Build menu, click Build Solution. You may receive the compilation errors described in the "Symptoms" section.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MajorLast Reviewed:8/7/2003
Keywords:kbCompiler kbprb kberrmsg kbJava KB327770 kbAudDeveloper