Dart 문법설명을 해보자
중요해보이는 문법 위주로 정리한다.
try catch
//try catch
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
}
print('finish!');
catch 에 잡힌후, 다음 구문 실행 됨.
try catch rethrow
//try catch rethrow
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
rethrow;
}
print('finish!');
rethrow를 만나고 프로그램 정지됨.
try catch finally
//try catch finally
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
rethrow;
} finally {
print('print finally!');
}
print('finish!');
rethrow를 만나도, finally 는 무조건 실행됨.
assert
//assert
var count1 = 10;
assert(count1 == 1); // => Exception has occurred
=>
Uncaught Error: Assertion failed
코드실행 하다 assert 문구 false 면 정지.
var count2 = 10;
assert(count2 == 10); // => pass
print('finish');
assert 문구 true이면 코드 통과
forEach
//forEach()
var collection1 = ['bmw', 'hyundai', 'kia', 'honda'];
collection1.forEach((element) {print('collection1 : ${element}');});
for in
//for in
var collection2 = ['bmw', 'hyundai', 'kia', 'honda'];
for(var element in collection2) {
print('collection2 : ${element}');
}
continue
//continue
var collection3 = ['bmw', 'hyundai', 'kia', 'honda'];
for(var i=0; i<collection3.length; i++) {
if(i>=2) continue; // => exit to for
print('i=${i}');
}
for문에서 continue 를 만나면 for문을 빠져나감.
break
//break
var i = 0;
while(true) {
i++;
print('i=$i');
if(i>=2) break; // => exit to while
}
while문에서 break를 만나면 while문을 빠져나감.
---- English ----
Let's explain Dart grammar
Focus on the most important grammar.
try catch
//try catch
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
}
print('finish!');
After being caught by catch , the next statement is executed.
try catch rethrow
//try catch rethrow
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
rethrow;
}
print('finish!');
Meets rethrow and program halts.
try catch finally
//try catch finally
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
rethrow;
} finally {
print('print finally!');
}
print('finish!');
Even if rethrow is encountered, finally is executed unconditionally.
assert
//assert
var count1 = 10;
assert(count1 == 1); // => Exception has occurred
=>
Uncaught Error: Assertion failed
Execute the code and stop if the assert statement is false.
var count2 = 10;
assert(count2 == 10); // => pass
print('finish');
If the assert statement is true, the code will pass.
forEach
//forEach()
var collection1 = ['bmw', 'hyundai', 'kia', 'honda'];
collection1.forEach((element) {print('collection1 : ${element}');});
for in
//for in
var collection2 = ['bmw', 'hyundai', 'kia', 'honda'];
for(var element in collection2) {
print('collection2 : ${element}');
}
continue
//continue
var collection3 = ['bmw', 'hyundai', 'kia', 'honda'];
for(var i=0; i<collection3.length; i++) {
if(i>=2) continue; // => exit to for
print('i=${i}');
}
If continue is encountered in the for statement, the for statement is exited.
break
//break
var i = 0;
while(true) {
i++;
print('i=$i');
if(i>=2) break; // => exit to while
}
When a break is encountered in a while statement, the while statement is exited.
댓글 없음:
댓글 쓰기