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
Source notes
MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 96, 35