Post

Java) static import / Exception Handling

Java) static import / Exception Handling

This post was migrated from Tistory. You can find the original here.

static import

1
2
3
4
import static java.lang.System.out;
import static java.lang.Math.*;

out.println(random());

A regular import lets you omit the package name, while import static lets you omit the class name when calling a static member.

Exception handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Main {
    public static void main(String[] args) {
        int input = 0;

        while (true) {
            try {
                System.out.println("1~100사이 값을 입력하세요");
                input = new Scanner(System.in).nextInt();
                System.out.println(input);
            } catch ( InputMismatchException e){
                System.out.println("Exception");
                System.out.println(e.getMessage());
                System.out.println(e.getStackTrace());
            }
        }
    }
}

Result
a
Exception
null
[Ljava.lang.StackTraceElement;@4fca772d

e.getMessage() returns null.
Scanner.nextInt() throws InputMismatchException(NumberFormatException.msg), and it seems the method that raises the NumberFormatException passes null as the message. (There’s probably a reason for that?)

Exceptions all trace back up to the Throwable class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Main {
    public static void main(String[] args) {
        int input = 0;

        while (true) {
            try {
                System.out.println("1~100사이 값을 입력하세요");
                input = new Scanner(System.in).nextInt();
                System.out.println(input);
            } catch ( CustomException e) { //CustomException is never thrown in the corresponding try block
                System.out.println("Exception");
                System.out.println(e.getMessage());
                System.out.println(e.getStackTrace());
            }
        }
    }
}

class CustomException extends Exception{
    CustomException(String msg){
        super(msg);
        System.out.println(msg);
    }
}

To use a custom exception, the try block needs a throw new CustomException(); somewhere in it.

As a general rule, handle exceptions with the closest common parent first, check whether there’s a relevant predefined exception (like InputMismatchException), and if none fits, create a custom exception and have your logic throw it.

deep copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Main {
    public static void main(String[] args) {
        int[][] arr = {{1, 2, 6}, {3, 4, 5}, {1, 1, 1}};
//        int[][] arr2 = Arrays.copyOf(arr, arr.length);
        int[][] arr2 = new int[arr.length][arr.length];
        for(int i=0; i<arr2.length; i++){
            arr2[i] = Arrays.copyOf(arr[i], arr[i].length);
        }
        arr2[2][2] = 3;
        arr2[0] = new int[]{3,3,3};
        System.out.println(Arrays.deepToString(arr));
        System.out.println(Arrays.deepToString(arr2));
    }
}

Result
[[1, 2, 6], [3, 4, 5], [1, 1, 1]]
[[3, 3, 3], [3, 4, 5], [1, 1, 3]]

//if commented out, this becomes a shallow copy
[[1, 2, 6], [3, 4, 5], [1, 1, 3]]
[[3, 3, 3], [3, 4, 5], [1, 1, 3]]

I should look for a deep copy method that’s actually usable in algorithm problems.

This post is licensed under CC BY-NC 4.0 by the author.