[컴][자바] Java의 Exception 과 RuntimException 의 차이

 

unchecked exceptions / runtime exception 을 조심히 써야 한다. / 쓰지 마라 /

Java의 Exception 과 RuntimException 의 차이

다음은 ref. 1, ref. 2의 글을 정리했다.

Java 에서 RuntimeException, Error 는 unchecked exception 들이다. [ref. 1]

Exception 은 compiler 가 check 한다. 하지만 RuntimeException 은 check 하지 않는다.(unchecked) 그래서 대체로 java 에서 exception 을 처리해야 하는 code를 넣지 않아도 compiler 가 error 를 보여주지 않는다. 물론 code 에서 RuntimeException 을 try...catch 로 잡아서 처리하는 것은 가능하다.(참고)

예를 들면, 아래와 같은 code

public static void read() throws FileNotFoundException {  
  
    FileReader file = new FileReader("C:\\myproj");  
    ...
}  

checked Exception 은 programming interface 의 하나이다. 누군가 method 를 만들때 parameter, return 값을 정의하는 것처럼 exception 을 정의해서 이것이 어떻게 동작하는지, 어떻게 사용하는지를 알려준다. (me: 예전의 c code 등에서 return 값의 error code 가 어떤 의미를 갖는지 설명해주는 것과 같은 용도로 볼 수 있겠다.)

RuntimeException 같은 unchecked Exception 은 그럼 왜 필요한가? 이것은 어떤 error 가 발생했을 때 API client code 가 합리적으로 그것에 대해 회복할 수 없거나, 어쨌거나, 그것을 다룰 수 없을 때, 예를 들면, ‘0으로 나누기’, null referene 로 object 에 대한 접근을 시도하는 등의 pointer exception, 잘못된 index 에 접근하는 index exception 등. (me: programmer 가 예상치 못한 error 등에 사용되는 exception 이라 보면 될 것 같다.)

그런데 이 RuntimException 을 모든 method 에 사용하는 것은 프로그램의 clarity 를 낮춘다. (me: 즉, program code 를 봐서는 어디서 exception 이 발생하는지를 알 수 없다.) 그래서 compiler 가 check 할 수 없다. 그중에 어떤 것은 check 돼야 하는 것임에도 불구하고 말이다.

RuntimeException 을 throw 하는 일반적인 예는, method 를 잘못 올바르지 않게 호출할 때이다. argument 로 null 이 들어가는 경우, method 는 NullPointerException 라는 unchecked exception 을 던질 것이다.

일반적으로 말하면, 단순히 당신의 method 들이 어떤 Exception 을 throw 해야 하는지 정의해야 하는 것을 하기 싫어서, RuntimeException 를 throw 하지 말라, 그리고 RuntimeException 의 subclass 를 만들지 마라.

최소한 맞춰야 할 점: - client 가 합리적으로 exception 으로부터 회복돼야 할 것으로 기대한다면, 이것은 checked exception 으로 만들라. - 만약, client 가 exception 으로부터 회복하기 위해 아무것도 할 수 없다면, 그것은 unchecked exception 으로 만들어라.

Reference

  1. Unchecked Exceptions — The Controversy (The Java™ Tutorials > Essential Java Classes > Exceptions)
  2. Extending Exception/RunTimeException in java? - Stack Overflow

[컴][웹] browser에서 파일로 csv file 을 읽어서, 화면에 뿌려주는 js 코드

js / javascript / 엑셀 업로드 / excel / csv upload / vue / vue code / csv upload

browser에서 파일로 file 을 읽어서, 화면에 뿌려주는 js 코드

아래는 vuejs code 이다.

// MyComponent.vue
<template>
  <div :class="klass">
    
    <input type="file" @change="onChangeInputFile"/>

    <h3>contents of the file:</h3>
    <pre>{{fileContent}}</pre>

  </div>
</template>

<script>

const _ = {
  name: 'MyComponent',
  components: {},
  props: {
    
  },
  data() {
    return {
      fileContent: "",
    }
  },
  methods: {
    onChangeInputFile(e){
      const file = e.target.files[0];
      if (!file) {
        return;
      }

      const isUtf = await _isUtf8(file)
      let encoding = ""
      if(!isUtf){
        encoding = "euc-kr"
      }

      const reader = new FileReader();
      reader.onload = (e) => {
        const contents = e.target.result;
        this.fileContent = contents
      };
      reader.readAsText(file, encoding);
    },
  },
}

function _isUtf8(file, callback) {
  const slice = file.slice(0, 10)
  const fr = new FileReader()

  const promise = new Promise(function (resolve, reject) {
    fr.onload = (e) => {
      // e.target.result : ArrayBuffer
      const view = new Uint8Array(e.target.result)
      // EF BB BF
      // 0xef, 0xbb, 0xbf
      let isUtf = false
      if (view[0] === 239 && view[1] === 187 && view[2] === 191) {
        isUtf = true
      } else {
        // do nothing
        isUtf = false
        // console.error('File has byte order mark (BOM)')
      }
      resolve(isUtf)
    }
  })
  fr.readAsArrayBuffer(slice)
  return promise
}
export default _
</script>

<style>
</style>

csv parser

See Also

  1. 쿠…sal: [컴][js] javascript 에서 file upload

Reference

  1. jquery - How to read data From *.CSV file using JavaScript? - Stack Overflow

[컴] encoding names and labels

 

인코딩 string / encoding string / str / 인코딩 레이블 / 인코딩 문자, 문자열

encoding names and labels

첫 링크를 보면, 아래처럼 보인다.

  • Nmae: “UTF-8”
  • Labels:
    • “unicode-1-1-utf-8”
    • “unicode11utf8”
    • “unicode20utf8”
    • “utf-8”
    • “utf8”
    • “x-unicode20utf8”

[컴] slack 의 DM 은 회사에서 볼 수 있을까?

사장이 볼 수 있나 / 대표가 / 회사가 내 메시지 볼 수 있나 / direct message in slack / monitoring / 슬랙 모니터링 / 사적인 대화를 슬랙에서?

slack 의 DM 은 회사에서 볼 수 있을까?

결론부터 이야기하면, 돈을 내면 볼 수 있다. 다만, 계약서등에 DM 도 보겠다는 내용이 명시되어 있어야 한다.

Exports: Workspace Owners and Admins can export data from public channels. Workspace Owners can also apply to access a self-serve data export tool. Access to this tool permits a Workspace Owner to export data from all channels and conversations, including private channels and direct messages, as needed and permitted by law. Each Workspace Owner must ensure that (a) appropriate employment agreements and corporate policies have been implemented, and (b) all use of data exports is permitted under applicable law.

2023-01-04 화면

[컴] WASM 과 container

 

웹어셈블리 /

WASM 과 container

Docker 는 WebAssembly 를 지원한다고 발표, runtime 으로 WasmEdge 를 사용한다.

WASI, WebAssembly System Interface

WASI is a modular system interface for WebAssembly.

WASI 는 WebAssembly 를 위한 modular system interface 이다.

다음 이미지를 보면 WASI 가 어떤 역할을 하는지 알 수 있다. 대략적으로 이야기하면, OS 가 가지고 있는 기능들을 사용할 수 있게 해주는 interface 라고 보면 된다. 그래서 WASM 은 이 WASI 를 통해서 OS 의 기능에 접근한다. 다른 이야기로 하면, Wasm Runtime 이 browser 에 있는 경우, browser의 기능만 사용하면 되기에 WASI 는 필요없지만, Wasm runtime 이 browser 가 아닌 os 에서 run 이 되는 경우라면, os 에서 제공하는 기능을 사용할 수 있어야 한다. 그래서 WASI 를 정의한 것이라 보면 되겠다.

Wasm Runtime 과 interpreted language

이것은 compiled language 는 그냥 wasm runtime 용으로 build 를 하면 됐지만, interpreted language 에서는 interpreter 를 wasm runtime 용으로 build 를 한다.

example

개인적인 생각

재밌다. 이전에 JVM 이 처음나왔을때, 하나의 code를 여러 platform 에서 사용할 수 있다는 점에서 좋았다. 그러다가 부족한 browser 의 기능을 커버하도록 web browser 에서 JVM 을 plugin(?) 식으로 사용할 수 있었다.

wasm 도 이와 비슷한 모양새다. 다만 처음의 시작이 오히려 browser 였으며, 이 browser 에서 사용되는 이녀석을 WASI 등을 통해 OS 라는 platform 까지 확장하는 모양새다.

다만 ref. 1 에서 이야기처럼, 이 관점은 jvm 보다는 좀 더 큰 관점인 VM 에서 보는 것이 맞을 듯 하다. 이유는 wasm 이 여러 language 로 된 code를 하나의 결과물로 만들어 준다는 점에 있다. 결국 우리가 computer world 에서 사용하는 모든 program 들이 특정 언어(language) 로 만들어진 것이라면, 이 모든 language 를 compile 해서 ‘특정 vm’ 위에서 run 할 수 있다면, 그 특정 vm 은 결국 os 와 다름 아니다.

Reference

  1. WebAssembly: Docker without containers!
  2. Introducing the Docker+Wasm Technical Preview | Docker, 2022-10-24

[컴] 구글의 UX Report(CrUX) 가 발표하는 가장 유명한 웹사이트 순위

 

순위 알아보는 법 / 순위 어떻게 / rank 아는 / url 순위 / rank

구글의 UX Report(CrUX) 가 발표하는 가장 유명한 웹사이트 순위

구글의 UX Report(CrUX) 가 발표하는 가장 유명한 웹사이트 순위가 다른 top list 들보다 훨씬 정확도가 높다는 연구가 있다.

아래 github 에 cached 된 값이 올라와 있다.

이 리스트에선 대략적으로 rank 1000 까지는 같은 순위를 적어놨다. 즉, 자세한 순위는 알 수 없다. 그래도 전세계 1000위에 어떤 사이트가 있는지 등의 자료는 알 수 있다. 그리고 특정 site 가 어느정도로 유명한지도 확인할 수 있다.

참고로 1000위 안에 있는 naver url 들이다.

  • https://www.naver.com
  • https://m.naver.com
  • https://search.naver.com
  • https://m.search.naver.com
  • https://n.news.naver.com
  • https://sports.news.naver.com
  • https://finance.naver.com
  • https://m.comic.naver.com

daum kakao 의 url 은 다음과 같다.

  • https://m.daum.net
  • https://v.daum.net
  • https://m.cafe.daum.net
  • https://www.daum.net