개발 공부/알고리즘

HackerRank, c++, Cpp exception handling

그냥하는티스토리 2022. 11. 3. 15:32
728x90

c++ 에서 예외를 사용하는 것이 좋을지, 

error를 핸들링하는게 좋을지에 대한 고민이 필요 한 것으로 보인다.

실제 코드를 실행하는 performance 자체로만 놓고보면 if-else가 훨씬 우위에 있는 걸 볼 수 있다.

void process_input(int n) {
    try{
        int d = largest_proper_divisor(n);    
        cout << "result=" << d << endl;
    }catch(invalid_argument e) {
        cout << e.what() << endl;
    } 
    cout << "returning control flow to caller" << endl;
    
}

 

 

* 실제로는 조사가 필요해 보인다.

* 참고자료, https://www.codeproject.com/Tips/490765/If-else-instead-of-try-catch

 

If/else instead of try/catch

In this article I tell you how you to use if/else instead of try/catch.

www.codeproject.com

 

728x90