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

The 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.

Example

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) Class Objects 1 2) Class Objects 2 3) Class Objects 3

Program 1 Program 2 Program 3
Exception Example Weather Service

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