• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RuntimeException. How to handle?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a class called BusinessServices responsible for handling the major application transactions. This class has the methods: connect, searchFlights and bookFlight. Exceptions caught during processing of this methods are wrapped and thrown as a ClientException. I also have a class called ExceptionHandler responsible to handle ClientException exceptions caught in the client application. As of now, this handler only shows a message box with details about the exception. This is the pattern used in my code:

After a routine inspection in my projet, I realized that the RuntimeException thrown by the invariant() method in the Data class can propagate up to the client and never being caught because I am not catching RuntimeException, only ClientException. I dont know how to handle RuntimeException in a better way than showing a message and finishing the application.
Now the question. How should I handle RuntimeException? Catch it inside BusinessServices and wrap it in a ClientException just like the other checked exceptions? Or I should instead catch it explicitly like:
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marcos,

Now the question. How should I handle RuntimeException? Catch it inside BusinessServices and wrap it in a ClientException just like the other checked exceptions? Or I should instead catch it explicitly (...)


The second solution seems better to me.
In your ExceptionHandler.handle() method, there is no much difference :
Solution 1 :
----------
Its parameter is a ClientException, but you need to check its getCause() to decide if you finish the application after the message box.
Solution 2 :
----------
Its parameter is an Exception, but you need to check its type (instanceof) to decide if you finish the application after the message box.
But in the caller, I like that RuntimeExceptions are catched separately.
Just IMO, because both solutions are defendable.
Cheers,
Phil.
 
Marcos Motta
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Philippe,
By the way, you have mentioned getCause(). I am using jdk 1.3.1. I started the exam 1 year from now. I implemented a sort of exception chaining in my ClientException. I created a method called ClientException.getReason() instead of getCause() present in 1.4.
Do you think that I might have problems because of this?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcos,
My assignment says: "You may develop your code using any implementation of the Java 2 platform, but the submission that you return must have been tested and shown to work under a production (not development) version of the Sun Microsystems' Java 2 platform and that platform must not have been superseded by a new production version for more than 18 months by the time you make your submission."
kktec
SCJP1.4, SCWCD
reply
    Bookmark Topic Watch Topic
  • New Topic