Microsoft KB Archive/327770

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

Article ID: 327770

Article Last Modified on 8/7/2003

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



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(&quot;New CLR Exception&quot;); }     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
 Start Visual Studio .NET. Create a new Visual J# .NET console application named MyJSharpConsoleApplication.  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(&quot;New CLR Exception.&quot;); }       catch(Exception e)        { System.out.println(&quot;Attempting to catch CLR Exception using Exception : &quot; + e.ToString); }       finally {           System.out.println(&quot;This message is printed always!!!&quot;); }   } }  On the Build menu, click Build Solution. You may receive the compilation errors described in the &quot;Symptoms&quot; section.</ol>

<div class="references_section">