12/22/2015
The
finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.Note: If the JVM exits while the
try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
The
try block of the writeList method that you've been working with here opens a PrintWriter. The program should close that stream before exiting the writeList method. This poses a somewhat complicated problem because writeList's try block can exit in one of three ways.- The
new FileWriterstatement fails and throws anIOException. - The
list.get(i)statement fails and throws anIndexOutOfBoundsException. - Everything succeeds and the
tryblock exits normally.
12/26/2015
- try block - is the block where the code with which exception error could occur
catch block - could be 0 or many - is the block where details of what should be done when exception occurs.
Note: To write catch block, you should understand what type of exception your code in try block is going to throw. Ex: ArrayIndexOutofBoundException, ArithemeticException are different classes of exception that you should be aware of.
Note: Exception , is the global exception class that you could use if you are not aware of specific exception class that code in try block is going to throw.
Note: Exception , is the global exception class that you could use if you are not aware of specific exception class that code in try block is going to throw.
finally block - could be 0 or 1 - is the block that will execute when try block completes the execution.
- Syntax to stop the system execution is
System.exit --- this is used to stop the execution of the code.
- Java errors and exceptions are subclasses of 'Throwable' class in java.lang package.
Thus only object of type Throwable could be thrown in a code, including exceptions and system errors.
Ex:
java.lang.Throwable.Error
java.lang.Throwable.Exception
- Methods of Throwable class includes are: ,
getMessage - returns appropriate String error message from Throwable object
toString - returns the description of exception object, which includes exception type
initCause - sets the cause of the exception, which is always the another Throwable object
printStackTrace - to find out which method has thrown a particular exception
- Error vs Exception:
Error and Exception are direct subclasses of Throwable class.
Error subclass - is used serious system or compile time errors that cannot or should not be handled by an application. Errors that include are: ExceptionInitializeError, StackOverflowErr, NoClassDefFoundErr
Exception - is used for implementation specific exceptions that an application might be executed to handle for example: if a printer is switched off when the user attempts to print document.
- RuntimeException is a subclass that again contains ArithemeticException, IndexOutofBoundException, IllegalStateException, NegativeArraySizeException
- Unchecked Exception and Checked Exception:
Code that might trigger a runtime exception, but that does not contain try-catch blocks, will still compile. All exceptions deriving from RuntimeException are know as Unchecked exception. This means compiler does not check whether they are handled or declared.(why will compiler check???)
All classes that inherit from Exception and are not RuntimeException subclasses are known as checked exception classes.
-

Error vs Exception, the difference is?
ReplyDeleteError and Exception are direct subclasses of Throwable class.
DeleteError subclass - is used serious system or compile time errors that cannot or should not be handled by an application. Errors that include are: ExceptionInitializeError, StackOverflowErr, NoClassDefFoundErr
Exception - is used for implementation specific exceptions that an application might be executed to handle for example: if a printer is switched off when the user attempts to print document.
Why "application unable to open file" is considered as checked exception, and that is why it is mandatory to declare the exception that are caused by application??
ReplyDeleteWhy do we need to declare throws exception while declaring method, if there are chances of code in method generating exception?
ReplyDeleteWhat code creates and throws exception object?
ReplyDelete