SCJP – Quick Notes

FLOW CONTROL AND EXCEPTION HANDLING

STATEMENTS

Java Statements (JJ pg108)
Statement Description
empty consists of and performs no operation
block group of statements enclosed in {} . Treated as a single statement when used with other statements{ x +=y; if( x < 10 ) return y; }
declaration declares a variable with a particular type and optionally assigns a value: int x = 10;
labeled any statment may be labled using identifier:startLoop: for( ;; ){}
assignment evaluates an expression and assigns the result to a variable: x = y + z;
invocation calls an object method: s.toString();
return returns a value from a method call: return x;
Object creation creates a new instance of a given class: String s = new String(“abc”);
if..else selects between two alternativesif( a==b ) // do this else // do this
switch selects from various alternativesswitch( a ) { case 1: case 2: case 3: default:
for executes a set of statements for a defined number of iterationsfor( int i=0; i<10; i++ ) { // do this }
while executes a block of statements while a condition is truewhile( !done ) { // do this }
do executes a block of statments while a condition is falsedo { // this }while( !done );
break transfers the flow of control to a labeled block or out of an enclosing statement
continue forces a loop to start the next iteration
try-catch-finally catches and processes exception errors that occur during the execution of a given block of codetry { // some operation } catch (Exception e) { // handle the exception } finally { // do this }
throw throw an exception
synchronized gets a lock on an object and executes a statement blocksynchronized(obj){ obj.setProperty(x); }
  • in for statement there can be more than one initialization statement but the variables must either be declared outside the for-loop or the type for the variables must be declared at the beginning

Following compiles and runs ok:

for( int x=10, y=0; x>y; x–, y++){

System.out.println( x + “\t” + y);

}

Following produces compile error

int x; for( x=10, int y=0 ; x>y; x–, y++){

System.out.println( x + “\t” + y);

}

EXCEPTIONS

  • runtime exceptions are objects of the classes java.lang.RuntimeException java.lang.Error or their subclasses
  • runtime exceptions are also called unchecked exceptions
  • Code may also throw an exception using the throw statement
  • These are non-runtime or checked exceptions
  • Any exceptions you create in your code should extend java.lang.Exception which implements the interface java.lang.Throwable
  • an overriding method cannot throw any checked exceptions which are not part of the original methods throws clause
  • Static initializers, instance initializers, and class or variable initializers must not produce any checked exceptions
  • a compile-error occurs if the code included in the try statement will never throw one of the caught checked exceptions
  • if an exception is thrown, each catch clause is inspected in turn for a type to which the exception can be assigned; be sure to order them from most specific to least specific
  • a catch clause may throw another exception
  • if a finally clause is included, it’s statements are executed after all other try-catch processing is complete
  • If a catch clause invokes System.exit() the finally clause WILL NOT execute

Page Visitors: 3077

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *