![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
java
certification tutorial
|
|||||||||||||||||||||||||||||
|
chapter1 | chapter2 | chapter3 | chapter4 | chapter5 | chapter6 | |
|||||||||||||||||||||||||||||
|
Chapter4: Flow Control and Exception handling
*Java language supports several flow control statements such as shown in the table below:
switch case: This construct provides a highly efficient selecting between multiple alternatives.
*while: A while loop tests at the top.
*do - while: this block executes atleast once.
*for loop: the general form of a for loop is a for statement followed by a loop body.
*A comma seperated for a loop is allowed in the initial and increment sections,so that you can string together several initializations or increments.
*break: The break keyword when used with a loop halts execution of the current loop.
*continue: continue statement causes the flow of control to pass to the next iteration of the loop.
*break and continue with labels: *labels can be used to provide a target for continue and break. *continue does not jump to the labled statement but instead jumps to the end of the labled loop. *label identifiers can be reused as many times as long as they are not nested. *Identifiers used as labels can be any valid identifier. *Identifiers as labels do not conflict with a same identifier for a variable,method or class name.
Exception handling:
*only objects that are instances of throwable class are thrown by Java virtual machine or by throw statement. *Error and Exception are the direct descendents of the Throwable class. *Error class exceptions are handled by system. Instances of errors are internal errors and resource exhaustion inside the java runtime. *Errors are rare and usually fatal. *All user declared exceptions are subclasses of Exception. *By using exceptions to manage errors, java program has advantages like seperating Error Handling code from regular code, propagating errors up the call stock and grouping Error types and Error differentiation. *Runtime Exception is a subclass of Exception class. *Runtime Exception class represents exceptions that occur within the Java Virtual Machine (during runtime). *An example includes Null Pointer Exceptions, which occurs when a method tries to access a member of an object through a null reference. *When overriding a method in a subclass, the types listed in overriding method throws clause can be subset of the types listed in the overriden method throws clause. *It can throw fewer or no exceptions. *Checked and Unchecked Exceptions:
**Unchecked exceptions are Runtime Exception and any of its subclasses. *Class error and its subclasses also are unchecked. *The compiler won't insist that you declare each exception thrown by a method incase of an unchecked exception. *Checked exceptions must be caught at compile time. *Runtime exceptions do not need to be caught. *Handling exceptions: *The first step in writing an exception handler is to enclose the statements that might throw an exception within a try block. *The Try block governs the statements enclosed within it and defines the scope of any exception handlers. *ex:Try{ / / java statements } *A try statement must be accompanied by atleast one catch block or one finally block. ex:try{ / / java statements }catch(....) { .... }catch(...) { .... }.... *There can be no intervening code between the end of the try statement and the beginning of the first catch statement. *A catch statement requires a single formal argument. ex:catch(some throwable object variable Name){ //Java statements } *Statements in a catch block are executed if and when the exception handler is invoked. *try/catch block protect the code that contains the method that might throw an exception inside a try block. *A try block can have a finally clause after all the catch clauses. **The code block in a finally clause is always executed, unless the thread executing the try code dies. **The finally clause helps by giving a guaranteed way to dispose of system resources and otherwise cleanup before exiting a method.
*multiple catch classes can be used to a try statement . *Specific exception should come first and the common exception should come at the end otherwise a compile time error occurs. *throws clause: Instead of handling errors at the point where they are likely to occur,the error can be passed up the call stock by use of the throws clause when declaring a method. *The throws keyword is used to identify the list of possible exceptions that a method might throw. *To create and throw specific exception throw keyword is used. ex:throw new somekindofexception().
|