0%

Java Exception

Here is the basic idea to custom an Exception in Java.

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
class Main {
public static void main(String[] args) {
TreeNode test = new TreeNode(1);
try {
System.out.println(printNodeValue(null));
} catch (Exception e) {
e.printStackTrace();
}
}

public static int printNodeValue(TreeNode p) throws solutionException {
if (p == null) {
throw new solutionException("null treenode p");
} else {
return p.val;
}
}
}

class solutionException extends Exception {
solutionException(String s) {
super(s);
}
}

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode (int x) {
val = x;
}
}