Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

HOME

java certification tutorial

chapter1 | chapter2 | chapter3 | chapter4 | chapter5 | chapter6 |

chapter7 | chapter8 | chapter9 | chapter10 | chapter11 |

 

Chapter4: Flow Control and Exception handling

 

*Java language supports several flow control statements such as shown in the table below:

keyword
decision making if-else,switch-case
loop for,while,do-while
exception try-catch-finally,throw
miscellaneous break,continue,label:,return

 

  • if : The if conditional statement is used when you want to execute different code based on the result of a boolean test expression.
  • They contain the keyword 'if', followed by a boolean test, followed by either a single statement or a block of statements to execute if the test is true.
  • An optional 'else' keyword provides the alternative statement to execute if the test is false.
  • the test must return a boolean value(true,false) and not 0 and 1.

switch case: This construct provides a highly efficient selecting between multiple alternatives.

the syntax for this construct is

switch(expression){

case value1:statement(s); break;

case value2:statement(s); break;

default:statement(s);

}

 

  • The argument to a switch statement must be a byte,char,short or int and must be a constant or constant expression.
  • It takes only one argument.
  • If you omit a "break" at the end of a branch, after the branch is executed control falls through to execute all the remaining branches.
  • there can be only one 'default' branch.
  • If none of the cases match and there is no default case,the statement does nothing.
  • You can't use a reference variable in a switch or case statement.
  • You can't use a primitive variable in a case statement. only constants that the computer can evaluate at compile time are allowed.These can be literal constants or static final variables in a class that the compiler can find.
  • You can't use a long primitive value in switch or case statements unless you specifically cast it to an int or smaller primitive.
  • every case number must be unique.

*while: A while loop tests at the top.

  • the code in a while block may sometimes be never executed.

*do - while: this block executes atleast once.

  • this is a do version of while loop.

*for loop: the general form of a for loop is a for statement followed by a loop body.

for(initialization(s); logical test; update)

 

*A comma seperated for a loop is allowed in the initial and increment sections,so that you can string together several initializations or increments.

An infinite loop is
for( ; ; ) { //statements }

 

 

*break: The break keyword when used with a loop halts execution of the current loop.

  • if you have nested loops within loops,the break statement causes the control to pass to the next outer loop.

*continue: continue statement causes the flow of control to pass to the next iteration of the loop.

  • it is used when you have nested loops and you want to breakout of an inner one and start with the next iteration of the next 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:

  • An exception is an event that disrupts the normal flow of instructions.
  • In java terminology, creating an exception object and handling it to the runtime system is called throwing an exception.
  • All error and exception classes inherit from the Throwable class.

*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:

  • A checked exception is some subclass of Exception excluding class Runtime Exception and its subclasses.
  • Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.
  • Checked Exceptions are exceptions that are not runtime exceptions and are checked by the compiler, the compiler checks that these exceptions are caught or specified.

**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.

finally method also gets executed
*if the try block completed normally (no exception)
*if one of the catch blocks handled an exception

*if an exception occured that none of the catch block is handled.

 

*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().

 

take an exam on this topic here
Please submit your ideas on this topic here
sign my guest book here

 

Copyrights anilbachi.8m.com

All rights reserved