- This topic has 6 replies, 3 voices, and was last updated 20 years, 7 months ago by Riyad Kalla.
-
AuthorPosts
-
Marcus BeyerMemberThe generated EJB example method contains a throws declaration:
/** * An example business method * * @ejb.interface-method view-type = "remote" * * @throws EJBException Thrown if the instance could not perform * the function requested by the container because of an system-level error. */ public void replaceWithRealBusinessMethod() throws EJBException { // rename and start putting your business logic here }
What is the impact of that declaration?
What is the meaning of declaring a throw statement for an Exception that inherits from RuntimeException?thanx!
Marcus
Riyad KallaMemberSweet god in heaven… I just got done typing a 2 page reply with 5 code snippets and got logged out…. ok so here is my short answer:
a runtime exception does not require a try-catch block, think of Integer.parseInt. BUT throwing a runtime exception gives the developer an opportunity to catch any errors and recover gracefully, for example if you do:
int i = Integer.parseInt(“a”);you can catch the BadNumberFormat exception and assign i a default value, instead of leaving it uninitialized.
Same goes for EJBs, you try and do a myEJB.findUser(“blarg”); and get an exception, you can recover and display to your user an error like “The connection to the DB seems to be lost, please contact the system administrator”.
Marcus BeyerMemberoutch — timeout? 😯
I usually copy the edited text before pressing any buttons in browsers …Thank you! Although I already knew the effects of throwing exceptions and of catching them.
Still I have no idea what difference it makes to declare a throw statement on a method with RuntimeExceptions.
AFAIK “public void doSomething() throws RuntimeException” is completely equivalent to “public void doSomething()”
including catching etc.Marcus
Riyad KallaMemberAFAIK “public void doSomething() throws RuntimeException” is completely equivalent to “public void doSomething()”
Ahhh OK, I think I understand better. These two are not equivalent, they LOOK equivalent, and can be used in the same fashion but they are not equivalent. The “throws RuntimeException” is a contractual promise of the method telling the developer that “Hey, I MIGHT throw an exception, so you can catch it and do something better if you need to”. This is the same principle with interfaces, they LOOK the same as classes that don’t implement them, but they are not the same. The interface is a contractural (sp?) promise from the class that it will follow certain guidelines and have certain methods. For example:
public class MyNumber{ }
as opposed to
public class MyNumber implements Serializable{ }
They both look identical, but one of them can be serialized (send to disk, send over network using ObjectStream, etc.) and one of them will blow up when you try and serialize it. This is the same general idea with the throws clause. The developer has to decide what methods do and don’t throw exceptions… for example, say you have a method “getUser” that gets a DB conneciton, then performs a user query to retrieve the user via email address. You could of course catch the exceptions related to DB in this method, and return null… OR you could let the exceptions propogate up to the calling method, and let it handle the situation.
Using the user example, there are 2 problems that could happen… (1) the db connection cannot be made and (2) the SQL query to get the user is incorrect. Lets assume the two exceptions are “ConnectionException” and “SQLException”. If you catch those in the method, and just return null… you *might* be oversimplifying the error, so the calling method doesn’t know REALLY what went wrong, and doesn’t know what to do. If you throw the exception up a level to the calling method, it can have 2 appropriate catch blocks and handle each exception differently:
Consider the following:
Calling methodpublic User getUser() { User user = null; try { user = UserDAO.getUser("fred@internet.com"); } catch(ConnectionException e) { // DB connection is down and to try again later } catch(SQLException e) { // sql query is invalid, during testing this is helpful for a developer } }
UserDAO method:
public static User getUser(String email) throws ConnectionException, SQLException { ... impl details ... }
To close this thread (since it has nothing to do with ME a tthis point :)) I would say that clearly defined throws clauses help keep APIs clearly defined and maximize flexibility (when used appropriately). So while using an API that uses throws RuntimeException clauses and using one that doesn’t might look exactly the same, the API that throws the exceptions is going to be more flexible to handling errors and recovering from them than the other.
Marcus BeyerMemberThank you very much! I really learned something about Java today 🙂
Most interesting I just discovered that Eclipse generates catch blocks for RuntimeExceptions when you do “Surround with try/catch Block”. Example:
public static void doSomething() throws EJBException, RuntimeException { // ... } public static void main(String[] args) { try { doSomething(); } catch (EJBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
binyanMemberRiyad,
Your reply comes under the “above and beyond the call of duty” heading and you do justice to tech support everywhere. Your co-workers and management should be getting you your choice of beverage on bended knee even as I type this. Seriously, that wasn’t an ME issue and you still helped waaay out. If only we could get support from some of our vendors like that.
Riyad KallaMemberYour reply comes under the “above and beyond the call of duty” heading and you do justice to tech support everywhere. Your co-workers and management should be getting you your choice of beverage on bended knee even as I type this. Seriously, that wasn’t an ME issue and you still helped waaay out. If only we could get support from some of our vendors like that.
I really appreciate the kind words binyan. I try to give the support that I would love to get… its also always upset me when I see someone perfectly capable of answering a question, just shutting someone out. Now if the original question had been “Will Bush beat Kerry in the elections?” then I would have directed mb over to the Eclipse forums for help 😀
-
AuthorPosts