Local database reading edition

Exception Handling

Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.

Forum index · Java - Tips Of The Day

Opening post

Padma · 2001-04-17 20:08:14

Exception handling is interesting to understand to write proper code. Java's exception mechanism provides a structured way to perform a go-to from the place where an error occurs to the code that knows how to handle the error. Exceptions extends from Throwable and it has two direct sub classes; Exceptions and Error. Exceptions are a result of abnormal conditions that can often be handled by some catcher in the stack. If it is not caught, it could result in ending the process. Exceptions versus errors Errors are usually thrown for more serious problems, such as OutOfMemoryError, that may not be so easy to handle. In general, code you write should throw only exceptions, not errors. Errors are usually thrown by the methods of the Java API, or by the Java virtual machine itself. It is possible to use java-defined exceptions or create new exception extending the java.lang.Exceptions class. Here is a method doSomething in some class throws and IllegalArgumentException exception . Sample Code Block 1 doSomething(String s) throws IllegalArgumentException { if (s > 100 ) throw new IllegalArgumentException(“Expected Patameter is invalid ”+s); } Sample Code Block 2 try { doSomething(150) // it throws IllegalArgumentException } catch(IllegalArgumentException e) { System.out.println(“Invalid Argument ”+ e); } Above example how you can catch an exception. See the following example, shows how it catch the RunTimeException. But in this case second catch clause would never be reached. Sample Code Block 3 try { doSomething(150) // it throws IllegalArgumentException } catch(RunTimeException r) { System.out.println(“Invalid Argument ”+ r); } catch(IllegalArgumentException e) { System.out.println(“Invalid Argument ”+ e); } The reason is the IllegalArgumentException is a subclass of a RunTimeException. The rule here is subclass catch clauses must precede superclass catch clauses. Then what is the Checked vs Unchecked exceptions Checked Exception somee subclass of Exception or Exception itself, excluding class RuntimeException and its subclasses. If a checked exception is throwing , you will need to declare the exception in your method's throws clause see the sample code block 1. Client programmers that calls call method that throws the exception will then need to either catch and handle the exception within the body of their methods, or declare the exception in the throws clause of their methods. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. Unchecked Exception RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked, but when throwing an exception we need focus on Exceptions. If an unchecked exception is throwing, client programmers can decide whether to catch or disregard the exception, just as with checked exceptions. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. Either way, client programmers are less likely to think about what they should do in the event of an unchecked exception than they are in the case of an checked exception. · When you throw an exception and if you feel client programmers should consciously decide how to handle, throw a checked exception. Padma

Reply 35

Asha · 2001-04-27 15:01:59

<h3>Use exceptions where you really need them</h3> <b>Exceptions not only have high basic cost, but their presence can hurt compiler analysis.</b> Just as it can be more difficult for people to read code that has try-catch blocks, it is more difficult for a compiler to analyze such code. In terms of legibility, however, try-catch blocks can actually be beneficial, because they describe the types of problem situations the software is expected to manage and how it is expected to manage them. This mechanism allows you to deal with problems as close to their source as you choose. At runtime, the presence of try-catch blocks doesn't significantly affect performance -- except when an exception occurs. In such situations, however, you must handle the exception anyway, so the cost is tolerable. Only when the situation is truly exceptional should you use this mechanism, though you may be tempted to throw exceptions simply to take advantage of their control-flow aspects. A good use of this mechanism would be to throw an exception if a file could not be found; a poor use would be to throw an exception with the name of a file that had been found. In the former case, the cost of the mechanism would only be incurred when the app was unable to proceed normally; in the latter case the cost would be incurred all the time without any added benefit. <BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote> Java's exception mechanism provides a structured way to perform a go-to from the place where an error occurs to the code that knows how to handle the error. Exceptions are a result of abnormal conditions that can often be handled by some catcher in the stack. If it is not caught, it could result in ending the process. when you throw an exception and if you feel client programmers should consciously decide how to handle, throw a checked exception. <hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote> Asha Balasubramanyan http://www.ammu.org Edited by - asha on April 27 2001 3:03:08 PM

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 96, 35