Repository navigation
Expand file tree
/
Copy pathJavaPassByValueExample1.java
More file actions
48 lines (38 loc) · 1.74 KB
/
Copy pathJavaPassByValueExample1.java
File metadata and controls
48 lines (38 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package devsought;
import java.util.concurrent.ThreadLocalRandom;
public class JavaPassByValueExample1 {
public static void main(String... args) {
int random = ThreadLocalRandom.current().nextInt(1, 100);
System.out.println(random);
JavaPassByValueExample1 byValueExample1 = new JavaPassByValueExample1();
byValueExample1.processTaskOne("Param1Value", "Param2Value", random);
}
public void processTaskOne(String param1, String param2, int val) {
//print out parameters
System.out.println("Param1:" + param1 + ", Param2:" + param2 + ", Value:" + val);
//simulate some work
boolean exceptionOccured = false;
System.out.println("Before subtask execution(exceptionOccured)=" + exceptionOccured);
processSubTaskOne(param2, val, exceptionOccured);
System.out.println("After subtask execution(exceptionOccured)=" + exceptionOccured);
if (exceptionOccured) {
System.err.println("There was an error processing subtask");
} else {
System.out.println("Processed subtask successfully.");
}
}
private void processSubTaskOne(String param, int val, boolean exceptionOccured) {
try {
if (val > 50) {
//throw exception
throw new Exception("Subtask One val out of range:" + val);
} else {
//normal value range processing
System.out.println("Normal processing for param:" + param + " value " + val);
}
} catch (Exception ex) {
System.err.println("Error occured in subtask with param " + param + " of value " + val + ", Error message:" + ex.getMessage());
exceptionOccured = true;
}
}
}