본문 바로가기
2015.08.22 16:35

if vs. try/catch overhead

조회 수 977 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://stackoverflow.com/questions/3490770/which-is-faster-try-catch-or-if-else-in-java-wrt-performance


It's not a question of which is faster, rather one of correctness.


An exception is for circumstances which are exactly that, exceptional.


If it is possible for n to be null as part of normal business logic, then use an if..else, else throw an exception.


----------------------------------

http://stackoverflow.com/questions/8621762/java-if-vs-try-catch-overhead


you really should not use try/catch and if interchangeably.


try/catch is for things that go wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try/catch.


if statements should be normal flow and ordinary error checking. So, for example, user fails to populate a required input field? Use if for that, not try/catch.


----------------------------------

http://www.codeproject.com/Tips/490765/If-else-instead-of-try-catch

Why if/else and not try/catch? 

Speed 

If you've one if/else block instead of one try/catch block, and if an exceptions throws in the try/catch block, then the if/else block is faster (if/else block: around 0.0012 milliseconds, try/catch block: around 0.6664 milliseconds). If no exception is thrown with a try/catch block, then a try/catch block is faster. But if you use 100 try/catch blocks in your program, and if one exceptions throws, then 100 if/else blocks is faster.

Skipping immediately

If you've a try/catch block where you divide by zero, and you do a few things before you divide, then some useless code is running. If you've a if/else block, then all useless code is skipped immediately, then you don't need to wait until the division.


Title
List of Articles
번호 제목 글쓴이 날짜 조회 수
3 When should we create our own java exception classes? Hojung 2015.08.22 958
» if vs. try/catch overhead Hojung 2015.08.22 977
1 Java Programming Cheatsheet Hojung 2015.06.27 352
Board Pagination ‹ Prev 1 Next ›
/ 1

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5