Professional Documents
Culture Documents
Z00040000120144010COMP6178 Session09 - Jump Operation and Exception Handling
Z00040000120144010COMP6178 Session09 - Jump Operation and Exception Handling
Z00040000120144010COMP6178 Session09 - Jump Operation and Exception Handling
Jump Operations
Additional control for Looping.
3 jump operations:
break: Stop (and quit) from inner looping,
continue: Stop (not quit) from looping,
generally using with if
label: Controlling exit for break and continue
break had been used at switch-case too.
Bina Nusantara
break Operation
Can be used in loop statements to provide
additional controls.
Simplify programming in some cases.
Caution!
Overusing or improperly using them, however,
can make programs difficult to read and debug
Bina Nusantara
Break Operation
Example:
for(int i=1; i<=3; i++)
{
for(int j=1; j<=3; j++)
{
if(j==2)
break;
System.out.println("i="+i+" and j="+j);
}
}
Bina Nusantara
Break Operation
for(int i=1; i<=3; i++)
{
for(int j=1; j<=3; j++)
{
if(j==2)
break;
System.out.println("i="+i+" and j="+j);
}
}
Bina Nusantara
Bina Nusantara
continue Operation
Can also be used in loop statements.
When it is encountered, it ends the current
iteration.
Program control goes to the end of the
loop body.
Always inside a loop.
Bina Nusantara
10
11
Bina Nusantara
12
break Vs continue
break keyword breaks out of the loop.
continue breaks out of the current iteration in
the loop.
Note:
In Java, the break and continue is different from go to
statement, which is provided in some programming
languages. They operate only in a loop or switch
statement.
Bina Nusantara
13
Bina Nusantara
14
Exception Handling
3 error type:
Syntax errors (compile errors) against syntax rule of coding
programming, founded at compile process by compiler
Logic errors (bug) logic errors, obtain deviate output/performance
Runtime errors false operation at program execution, program
terminated
Bina Nusantara
Exception
Runtime errors:
Occur while a program is running if the environment
detects an operations that is impossible to carry out.
Caused by exceptions.
Exception:
An object that represent an error or a condition that
prevents execution from proceeding normally.
Bina Nusantara
16
Exception Handling
Handling runtime errors (exception handling)
Using try and catch
Type of error generally occur are:
In-correct input
Arithmetic (divided by 0)
Surpass array boundary that had been set
Un-initialized Object
Bina Nusantara
Bina Nusantara
Bina Nusantara
19
Arithmetic Exception
Null Pointer Exception
Index Out of Bounds Exception
Illegal Argument Exception
Etc.
Bina Nusantara
20
Bina Nusantara
21
Declaring Exceptions
Every method must state the types of checked
exceptions it might throw.
Java does not require that you declare Error and
RuntimeException (unchecked
exceptions)explicitly in the method.
E.g.
public void myMethod()
throws Exception1, Exception2, , ExceptionN
Bina Nusantara
22
Throwing Exceptions
A program that detects an error can create an instance of an
appropriate exception type and throw it. (throwing an
exception)
Example:
A program pass a value to a method and the argument must be nonnegative, but a negative argument is passed.
The program can create an instance of IllegalArgumentException and throw
it as follows:
OR
Bina Nusantara
23
Catching Exceptions
When an exception is throw, it can be caught and
handled in a try-catch block.
try {
//Statement
}
catch (Exception1 exVar1){
//handle exception1
}
catch (Exception2 exVar2){
//handle exception2
}
24
Syntax?
try{
//statement
}
catch(TheException ex){
//handling exception
}
finally{
//final statement
}
Bina Nusantara
25
Bina Nusantara
26
27
Advanced Learning
Disadvantages try and catch:
Takes much time at executed.
Need more memory
Need handler searching
Bina Nusantara
References
Introduction to Java Programming. 8ed. Liang. 2011.
Chapter 13
The Complete Reference. 5ed. Herbert Schildt. 2005.
Chapter 10
Dasar Pemrograman Java 2. 2004. Abdul Kadir. Chapter
14
Jump Operation:
http://www.java2s.com/Code/Java/Language-Basics/ContinueWithLabelDemo.htm
http://www.java2s.com/Code/Java/Language-Basics/BreakWithLabelDemo.htm
Exception Handling:
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
http://www.javabeginner.com/java-exceptions.htm
http://rotterdam.ics.uci.edu/info/ExceptionHandler.htm
Bina Nusantara