The checked is a direct sub-class of exception class.
The un-checked is a direct sub-class of runtime exception class.
The error is a direct sub-class of error class
In my understanding, checked for the fault due to user; un-checked due to the fault of developer bad coding; error for the Java VM's faults.
The checked is foreseen by the author, and must be handled in try-catch block, or throws to the caller.
The un-checked may not be caught, even not be declared in the method signature.
The error should be left to the JVM to handle.
All exceptions are derived from throwable class, which is direct sub-class of an object class.
Custom Exception - Exceptions - Throwable - Object
IllegalStateException - RuntimeException - Exceptions - Throwable - Object
Error - Throwable - Object
Note: You may catch un-checked or error, or re-throw them. This won't be a fault. Normally, error is handled by JVM; (How runtime Exception is handled? I need figure out)
Checked exception is foreseen by the writer, or say it is defined by a developer who predicts on such a condition this exception should pop out. For instance, file not found exception. As file is not found in runtime, it needs to change current process and turning to another process, so that the program can be terminated elegantly or present the user another process. In some sense, this is the meaning why ask developer to predict and manage an exception, which need to be checked.
Un-checked exception happens on an existing code, which is not correctly used. For instance, index out of bound exception, class definition is not found, illegal state exception, illegal argument format, number format invalid and null pointer exception. In my understanding, when this kind of exception happens, it is hard to get program quit in a good manner. In a simple word, it is not a fault due to user inputs or whatever. This is the problem due to the code.
Errors happens with JVM, mostly due to the resources running out, and it cannot be recovered. for instance stack overflow exception, or others like the running out permanent memory or heap memory etc. It is critical, and requires the program to be terminated immediately.
How to declare a custom exception. it must be checked.
public class MyException extends Exception { public MyException() { super(); } public MyException(String message) { super(message); } }So, checked exception requires developer to provide handler in a try {...} catch {...} finally{}.
Throwing Exception: throwable can be thrown.
if not handled then throw new MyException("msg") to the caller.
Please note: 1) any throwable sub-class can be thrown. it doesn't means only custom exception can be thrown. 2) catch exception from the bottom of the exception inheritance structure; otherwise, it will cause unreached catch block; this will be a compiling error. 3) it is not a good practice to catch RuntimeException or Exception base classes. 4) you should not empty catch, at least having a finally block;
Exception alter Program Flow
1) once an exception thrown and caught, the rest of code will be over-skipped. So some code lines may not be implemented anyway, for instance, return clause. This should cause a compiling error, i.e. something like " un-reachable code".
try-catch
try-finally
try-catch-finally
try-with-resources: file IO or network streaming are considered as resources. They can be closed automatically.
multiple-catch: exception should not have direct inheritance relationship. Otherwise it will give a compiling error, i.e. "Alternatives in a multi-catch statement cannot be related by sub classing". The reason is because the sub-class exception handler won't be triggered, for it has been caught and handled in its generic type. You may combine single catch and multi-catch together to solve this issue. In addition, in a multiple catch clause, it needs to use a single exception variable, but not multiple exception variables. Otherwise, it causes a compiling error.
public void setAddress(Address address) { if (address != null) { try { GPSCoordinate gPSCoordinate = GoogleGeoCoder.getGPSCoordinate(address); setCoordinate(gPSCoordinate); } catch (InvalidKeyException | IOException | GoogleGeoCoderException ex) { Logger.getLogger(Yard.class.getName()).log(Level.SEVERE, null, ex); } } }
Please note: whatever finally block will be implemented, if the program is not terminated before it; for instance, system exist.
No comments:
Post a Comment