728x90
반응형

1. try-catch finally 블록
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
/**
 *      try-catch문
 *      에러가 발생할 수 있을만한 코드를 try 문에 작성하게되면
 *      예외가 발생하면서 catch문에 있는 코드를 수행하고
 *      finally는 에러가 발생하든 안하든 무조건 수행한다!
 * */
var sports,
    result;
 
try {
    console.log('try');
    sports = swim;  // 존재하지 않는 swim 변수를 sports 에 할당 하면 에러가 발생
catch(e) {
    result = 'error';
    console.log('catch');
} finally {
    console.log('finally');
    console.log(result);
}
 
 


2. throw를 통한 명시적 예외 발생 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
/**
 *      try-catch문
 *      에러가 발생할 수 있을만한 코드를 try 문에 작성하게되면
 *      예외가 발생하면서 catch문에 있는 코드를 수행하고
 *      finally는 에러가 발생하든 안하든 무조건 수행한다!
 * */
var sports,
    result;
 
try {
    if(!sports) {
        throw 'sports에 값이 없습니다.'
    }
    result = sports;
catch(e) {
    console.log(e);
    console.log(result);
}
 


728x90
반응형
728x90
반응형

JavaScript for~in문의 이해

JavaScript에서의 for~in문은 Object 에 존재하는 모든 property를 변수에 담는다. 
for~in문에서 가져온 Object의 속성은 순서대로 해당 변수에 담는다는 보장이없으므로 
순서가 중요할경우에는 for문을 사용하여 작성하여야 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 *     for~in 문 은 오브젝트에 포함된 property를 반복하면서 변수에 담는다.
 *     sports 에서 soccer , basketball 로
 *     값을 할당하였지만 for~in 문은 내가 값을 넣은 순서대로
 *     나온다는 보장이 없다.
 *     sort로 사용하려면 배열을 사용한 일반 for문을 사용해야한다.
 * */
 
var sports = {soccer: 11, basketball: 5};
 
for(var pty in sports) {    //  for~in 문은 순서를 가지고 있지 않는다.!!!중요하다
 
    var value = sports[pty];
 
    console.log('name : ' + pty + ' , value : ' + value);
 
}

728x90
반응형
728x90
반응형

JavaScript 의 핵심 키워드 2가지

JavaScript 를 이해하기 위해서는 JavaScript가 할 수있는일에 대한 범위,영역를 알아야한다. 


1. 제어 (controll) : HTML,CSS 등을 오브젝트화 시켜서 제어하는것이 목적이다.

- DOM, HTML, CSS

- HTML5 API File ,Messaging 메카니즘

- 그래픽 SVG, Canvas, WebGL

- 통신 XML( HttpRequest Socket )

2. 수단 Object[name:value] : 오브젝트의 네임밸류 속성을통하여 제어함. 

- 자바스크립트의 근간인 Object는 네임,밸류 이다!!! 

- function toDay() {} // toDay 라는 name 에 function 이라는 value 다.




728x90
반응형
728x90
반응형

간단한 배치모듈을 구성중 외부 설정파일(properties) 에 대해 원하는 키를 인자로 넣어 값을 리턴하는 클래스가 필요하여 

간단하게 구성한 클래스이다. 

이 클래스의 예제 에서는 getString(),getBoolean(),getLong(),getInt() 와 getObject()를 이용하여 설정값을 가져오고있다.

이를 응용하여 다른 자료형으로 반환하는 메서드를 만들어 사용해도 될것이다.


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 *      create by sanghoon lee
 *
 * */
package util;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
 
public class ConfigInjector {
 
    private static ConfigInjector config = new ConfigInjector();
    final static String CRLF = "\n\r";
    Properties props;
 
    private ConfigInjector() {
        init();
    }
 
    private void init() {
        loadConfig();
    }
 
    /**
     * properties load..
     * */
    public void loadConfig() {
        props = new Properties();
        try {
            if(isWindow()) {    //    윈도우인 경우..
                 props.load(
                         new BufferedReader(
                         new FileReader(
                         new File( "properties/batch.properties" )
                         )));
            }
            else {    //    윈도우가 아닌경우..
                props.load(new BufferedReader(new FileReader(System
                        .getProperty("batch.properties"))));    
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public boolean isWindow() {
        String os = System.getProperty("os.name");
        if(os.indexOf("Window") != -1) 
            return true;    //    윈도우 일경우 
        return false;    //    윈도우가 아닐경우
    }
    
    /**
     * load 한 프로퍼티 파싱
     * */
    public String parseConfig(String key) {
        String value = "";
        try {
            value = props.getProperty(key);
        } catch (Exception e) {
            // ignore
        }
        return value;
    }
 
    /**
     * propertis 를 읽어와 Object 으로 리턴 getObject 에서는
     * */
    public Object getObject(String key) {
        String value = "";
        try {
            value = parseConfig(key);
        } catch (Exception e) {
            // ignore
        }
        return value.trim();
    }
 
    /**
     * properties 를 읽어와 String 으로 리턴
     * */
    public String getString(String key) {
        String value = "";
        try {
            value = (String) getObject(key);
        } catch (ClassCastException e) {
            // ignore
        }
        return value;
    }
 
    /**
     * properties 를 읽어와 boolean 으로 리턴
     * */
    public boolean getBoolean(String key) {
        boolean flag = false;
        try {
            flag = Boolean.parseBoolean((String) getObject(key));
        } catch (ClassCastException e) {
            // ignore
        }
        return flag;
    }
 
    /**
     * properties 를 읽어와 int 으로 리턴
     * */
    public int getInt(String key) {
        int result = 0;
        try {
            result = Integer.parseInt((String) getObject(key));
        } catch (ClassCastException e) {
            // ignore
        }
        return result;
    }
 
    /**
     * properties 를 읽어와 Long 으로 리턴
     * */
    public long getLong(String key) {
        long result = 0;
        try {
            result = Long.parseLong((String) getObject(key));
        } catch (ClassCastException e) {
            // ignore
        }
        return result;
    }
 
    /**
     * ConfigInjector 인스턴스를 리턴
     * */
    public static ConfigInjector getInstance() {
        return config;
    }
 
    /*
     * public static void main( String[] args ) { ConfigInjector config =
     * ConfigInjector.getInstance(); String pw =
     * config.getString("copydb.userpw"); String dir =
     * config.getString("batchjob.log.dir"); System.out.println( pw );
     * System.out.println( dir ); ConfigEntity entity =
     * config.getConfigEntity(); System.out.println(entity); }
     */
 
}


728x90
반응형

+ Recent posts