Lesson 7 | Mapping for exceptions, part 2 |
Objective | Describe and use the Java produced by the mapping for user-defined IDL exceptions. |
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.
// Exception example - Weather Service module Module4 { exception NoReportForCity { string closestKnownCity; }; interface WeatherService { string getForecast(in string city) raises (NoReportForCity); }; }
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; } }
// Exception example - Weather Service module Module4 { exception NoReportForCity { string closestKnownCity; }; interface WeatherService { string getForecast(in string city) raises (NoReportForCity); }; }
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; } }
// Exception example - Weather Service module Module4 { exception NoReportForCity { string closestKnownCity; }; interface WeatherService { string getForecast(in string city) raises (NoReportForCity); }; }
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; } }
// ... 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."); } }