IDL Constructed Types   «Prev  Next»
Lesson 7 Mapping for exceptions, part 2
Objective Describe and use the Java produced by the mapping for user-defined IDL exceptions.

Java produced by the Mapping for User-defined IDL Exceptions

User-defined IDL exception Mapping

User-defined IDL exceptions map to final Java subclasses of org.omg.CORBA.UserException. The mapping for this is basically identical to that for structs because IDL exceptions are very much like structs. The Java code generated for exceptions is the same as that generated for structs, except for some more esoteric cases.
Add exception to another Weather Service
Let us add an exception to another variation of our Weather Service. Use the SlideShow below to see the IDL and generated Java code:

1) IDL Exception NoReportForCity is mapped to a corresponding Java class
1) IDL Exception NoReportForCity is mapped to a corresponding Java class
File 1: `exception.idl`
// Exception example - Weather Service
module Module4
{
    exception NoReportForCity
    {
        string closestKnownCity;
    };

    interface WeatherService
    {
        string getForecast(in string city) raises (NoReportForCity);
    };
}

File 2: Java class corresponding to the IDL exception
package Module4;

public final class NoReportForCity
    extends org.omg.CORBA.UserException
{
   public java.lang.String closestKnownCity;

   public NoReportForCity() {}

   public NoReportForCity(java.lang.String closestKnownCity)
   {
       this.closestKnownCity = closestKnownCity;
   }
}

The IDL file defines an exception for a weather service module and an interface that can raise this exception when trying to get a forecast. The Java class implements the exception with functionality to carry information about the closest known city.


2) The user defined exception is mapped to a subclass of org.omg.CORBA.UserException
2) The user defined exception is mapped to a subclass of org.omg.CORBA.UserException File 1: `exception.idl`
// Exception example - Weather Service
module Module4
{
   exception NoReportForCity
   {
       string closestKnownCity;
   };

   interface WeatherService
   {
       string getForecast(in string city) raises (NoReportForCity);
   };
}

File 2: Java class corresponding to the IDL exception
package Module4;

public final class NoReportForCity
    extends org.omg.CORBA.UserException
{
    public java.lang.String closestKnownCity;

    public NoReportForCity() {}

    public NoReportForCity(java.lang.String closestKnownCity)
    {
        this.closestKnownCity = closestKnownCity;
    }
}

The IDL file defines an exception for a weather service module and an interface that can raise this exception when trying to get a forecast. The Java class implements the exception with functionality to carry information about the closest known city.


3) The member closestKnownCity is mapped to a corresponding public field and an argument to the full constructor.
3) The member closestKnownCity is mapped to a corresponding public field and an argument to the full constructor. File 1: `exception.idl`
// Exception example - Weather Service
module Module4
{
   exception NoReportForCity
   {
       string closestKnownCity;
   };

   interface WeatherService
   {
       string getForecast(in string city) raises (NoReportForCity);
   };
}

File 2: Java class corresponding to the IDL exception
package Module4;

public final class NoReportForCity
    extends org.omg.CORBA.UserException
{
   public java.lang.String closestKnownCity;

   public NoReportForCity() {}

   public NoReportForCity(java.lang.String closestKnownCity)
   {
       this.closestKnownCity = closestKnownCity;
   }
}

The IDL file defines an exception for a weather service module and an interface that can raise this exception when trying to get a forecast. The Java class implements the exception with functionality to carry information about the closest known city.

Using Exceptions in Java

The generated Java exception classes are handled in the same way as any other Java exception classes. The server-side implementation throws the exceptions, and the clients must catch them. For example, a client to the sample interface above would have to make a call like this:
// ...
String prediction = null;
try{
 prediction = wService.getForecast("New York");
}
catch(NoReportForCity nor){
 try
 {
  prediction = wService.getForecast
    (nor.closestKnownCity);
 }
 catch(NoReportForCity nor2) {
  System.out.println("No reports available for NY or
    closest known city.");
 }
}

You have now learned how IDL exceptions are mapped to Java to generate corresponding Java code and how such generated code is used in your own code. In the next lesson, you will learn about the IDL-to-Java mapping for IDL unions and how to use the resulting Java.

Corba Mapping Exception - Exercise

Click the Exercise link below to use exception handling in the course project.
Corba Mapping Exception - Exercise

SEMrush Software