Вы находитесь на странице: 1из 29

T1446 Algorithm and Programming

Jump Operations and Exception


Handling
Lecture Class
Session 12

Jump Operations and Exception


Handling
Jump Operation Definition
Break operation
Continue operation
Label operation at break
Label operation at continue
Exception Handling Definition
try and catch
finally
Bina Nusantara
University

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);
}
}

At j==2, execution exit from inner looping


Value j==2, and j==3 not printed
Looping continued at i++

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

break Operation Example

Bina Nusantara University

break Operation in Nested Loops

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

continue Operation Example

Bina Nusantara University

11

continue Operation in Nested Loops

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

break and continue Labels


Label declared before break or continue.
Label terminated with ( : )

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

Runtime errors : exception


Exception cause terminate program
Example:
Customer A transfer money to the account of customer B, when
account had reduced and account B not yet increase, exception
terminate program had happen. Customer A loses his money.

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.

If the exception is not handled, the program will


terminate abnormally.

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

If error not handled / catch, than error will continued


to the next handling
Error not handled will cause program terminated.

Bina Nusantara

Exception Handling Overview (1)

Bina Nusantara

Exception-Handling Overview (2)


Solution?

Bina Nusantara

19

Runtime Exception Types

Arithmetic Exception
Null Pointer Exception
Index Out of Bounds Exception
Illegal Argument Exception
Etc.

Bina Nusantara

20

Exceptions Thrown by a Method

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
}

Getting Information from Exceptions


You can use printStackTrace(), getMessage(), toString(), and
getStackTrace() methods in obtain information from exception
objects.
Bina Nusantara

24

The finally Clause


When?
If you want some code to be executed regardless of
whether an exception occurs or is caught.

Syntax?
try{
//statement
}
catch(TheException ex){
//handling exception
}
finally{
//final statement
}
Bina Nusantara

finally can declare without catch

25

The finally Clause Example (1)

Bina Nusantara

26

The finally Clause Example (2)


Sample input and the output:

Bina Nusantara University

27

Advanced Learning
Disadvantages try and catch:
Takes much time at executed.
Need more memory
Need handler searching

Advantages try and catch:


Catch complex error
Easy to read and modified

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

Вам также может понравиться