17
Parallel Programming Parallel Programming Exceptions --- Examples Exceptions --- Examples Thomas Gross Thomas Gross Spring Semester 2010 Spring Semester 2010 Mar 4, 2010 Mar 4, 2010

Parallel Programming Exceptions --- Examples

Embed Size (px)

DESCRIPTION

Parallel Programming Exceptions --- Examples. Thomas Gross Spring Semester 2010 Mar 4, 2010. Martin Lanter Javimka im Inforum [email protected] www.lantersoft.ch www.java-forum.org. Solution of assignment 1. class Example1 { /* baseline */ - PowerPoint PPT Presentation

Citation preview

Page 1: Parallel Programming Exceptions --- Examples

Parallel ProgrammingParallel Programming

Exceptions --- ExamplesExceptions --- Examples

Thomas GrossThomas Gross

Spring Semester 2010Spring Semester 2010Mar 4, 2010Mar 4, 2010

Page 2: Parallel Programming Exceptions --- Examples

Martin LanterJavimka im Inforum

[email protected]

www.java-forum.org

Page 3: Parallel Programming Exceptions --- Examples

– 3 – Spring 2010

Solution of assignment 1

class Example1 {class Example1 {

/* baseline *//* baseline */

public static void main(String[] args) {public static void main(String[] args) {

int i;int i;

int tmp;int tmp;

/* iterate through the argument vector *//* iterate through the argument vector */

for (i = 0; i < args.length; i++) {for (i = 0; i < args.length; i++) {

tmp = Integer.parseInt(args[i]) + 1; tmp = Integer.parseInt(args[i]) + 1;

System.out.println(tmp); /* print out the result */System.out.println(tmp); /* print out the result */

}}

} }

}}

Page 4: Parallel Programming Exceptions --- Examples

– 4 – Spring 2010

/*This program takes any number of integer arguments and increases each of them by 1. It then outputs them line by line. This source code is intellectual property of ***. All attempts to copy, modify and/or redistribute this file will be prosecuted. Trespassers will be shot, survivors will be shot again. Don't f*** with the source or the source will f*** you. Polymorphic code rulez the world. You will never know.*/

Page 5: Parallel Programming Exceptions --- Examples

– 5 – Spring 2010

public class Assignment1 {

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

System.out.println(Integer.parseInt(args[i])+1);}

}}

Page 6: Parallel Programming Exceptions --- Examples

– 6 – Spring 2010

Page 7: Parallel Programming Exceptions --- Examples
Page 8: Parallel Programming Exceptions --- Examples
Page 9: Parallel Programming Exceptions --- Examples
Page 10: Parallel Programming Exceptions --- Examples

– 10 – Spring 2010

Example 2

class Example2 {class Example2 {

public static void main(String[] args) {public static void main(String[] args) {

int i;int i;

int tmp;int tmp;

/* iterate through the argument vector *//* iterate through the argument vector */

for (i = 0; i < args.length; i++) {for (i = 0; i < args.length; i++) {

try {try {

tmp = Integer.parseInt(args[i]);tmp = Integer.parseInt(args[i]);

if (tmp < 0) {if (tmp < 0) {

throw(new MyException("less than zero"));throw(new MyException("less than zero"));

}}

System.out.println(tmp); System.out.println(tmp);

}}

Page 11: Parallel Programming Exceptions --- Examples

– 11 – Spring 2010

Example 2

catch (MyException e) {catch (MyException e) {

System.out.println("Exception caught ...");System.out.println("Exception caught ...");

System.out.println(e.getMessage()); System.out.println(e.getMessage());

}}

} }

System.out.println(“done.”)System.out.println(“done.”)

}}

}}

class MyException extends Exception {class MyException extends Exception {

MyException(String text) {MyException(String text) {

super(text);super(text);

}}

}}

Page 12: Parallel Programming Exceptions --- Examples

– 12 – Spring 2010

Example2

Input: 8, 3, 11, -4, 6, -7, 2, 0Output:

8311Exception caught ...less than zero6Exception caught ...less than zero20done.

Page 13: Parallel Programming Exceptions --- Examples

– 13 – Spring 2010

Example 5

class Example5 { class Example5 {

public static void main(String[] args) { public static void main(String[] args) {

int i; int i;

int tmp; int tmp;

int count = 0;int count = 0;

try {try {

/* iterate through the argument vector */ /* iterate through the argument vector */

for (i = 0; i < args.length; i++) {for (i = 0; i < args.length; i++) {

Page 14: Parallel Programming Exceptions --- Examples

– 14 – Spring 2010

Example 5

try {try {

tmp = Integer.parseInt(args[i]);tmp = Integer.parseInt(args[i]);

if (tmp < 0) {if (tmp < 0) {

throw(new MyException1("less than zero"));throw(new MyException1("less than zero"));

}}

System.out.println(tmp); System.out.println(tmp);

} catch (MyException1 e) {} catch (MyException1 e) {

System.out.println("Exception1 caught ...");System.out.println("Exception1 caught ...");

System.out.println(e.getMessage()); System.out.println(e.getMessage());

count++;count++;

if (count >= 3) {if (count >= 3) {

throw(new MyException2 ());throw(new MyException2 ());

};};

} }

Page 15: Parallel Programming Exceptions --- Examples

– 15 – Spring 2010

Example 5

finally {finally {

System.out.println("Processed one integer");System.out.println("Processed one integer");

}}

}}

} catch (MyException2 e) {} catch (MyException2 e) {

System.out.println("Exception2 caught");System.out.println("Exception2 caught");

System.out.println("Enough.");System.out.println("Enough.");

}}

finally {finally {

System.out.println("done."); System.out.println("done.");

}}

}}

} }

Page 16: Parallel Programming Exceptions --- Examples

– 16 – Spring 2010

Example 5

class MyException1 extends Exception {class MyException1 extends Exception {

MyException1(String text) {MyException1(String text) {

super(text);super(text);

}}

}}

class MyException2 extends Exception {}class MyException2 extends Exception {}

Page 17: Parallel Programming Exceptions --- Examples

– 17 – Spring 2010

8Processed one integer3Processed one integerException1 caught ...less than zeroProcessed one integerException1 caught ...less than zeroProcessed one integer5Processed one integerException1 caught ...less than zeroProcessed one integerException2 caughtEnough.done.

Example5

Input: 8, 3, -11, -4, 5, -2, 6

Output: