2022년 10월 27일 목요일

[Flutter] Dart 문법설명 (try catch, rethrow, finally, assert, forEach, for in, continue, break)

Dart 문법설명을 해보자

중요해보이는 문법 위주로 정리한다.


try catch

//try catch
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
}
print('finish!');
=>
e : NoSuchMethodError: method not found: '$add' on null
finish!

catch 에 잡힌후, 다음 구문 실행 됨.


try catch rethrow

//try catch rethrow
try {
var num = null;
num ++;
} catch (e) {
print('e : ${e.toString()}');
rethrow;
}
print('finish!');
=>
e : NoSuchMethodError: method not found: '$add' on null
Uncaught TypeError: Cannot read properties of null (reading '$add')Error: TypeError: Cannot read properties of null (reading '$add')

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!');
=>
e : NoSuchMethodError: method not found: '$add' on null
print finally!
Uncaught TypeError: Cannot read properties of null (reading '$add')Error: TypeError: Cannot read properties of null (reading '$add')

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');
=>
finish

assert 문구  true이면 코드 통과


forEach

//forEach()
var collection1 = ['bmw', 'hyundai', 'kia', 'honda'];
collection1.forEach((element) {print('collection1 : ${element}');});
=>
collection1 : bmw
collection1 : hyundai
collection1 : kia
collection1 : honda


for in

//for in
var collection2 = ['bmw', 'hyundai', 'kia', 'honda'];
for(var element in collection2) {
print('collection2 : ${element}');
}


=>
collection2 : bmw
collection2 : hyundai
collection2 : kia
collection2 : honda


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}');
}
=>
i=0
i=1

for문에서 continue 를 만나면 for문을 빠져나감.


break

//break
var i = 0;
while(true) {
i++;
print('i=$i');
if(i>=2) break; // => exit to while
}
=>
i=1
i=2

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!');
=>
e : NoSuchMethodError: method not found: '$add' on null
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!');
=>
e : NoSuchMethodError: method not found: '$add' on null
Uncaught TypeError: Cannot read properties of null (reading '$add')Error: TypeError: Cannot read properties of null (reading '$add')

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!');
=>
e : NoSuchMethodError: method not found: '$add' on null
print finally!
Uncaught TypeError: Cannot read properties of null (reading '$add')Error: TypeError: Cannot read properties of null (reading '$add')

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');
=>
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}');});
=>
collection1 : bmw
collection1 : hyundai
collection1 : kia
collection1 : honda


for in

//for in
var collection2 = ['bmw', 'hyundai', 'kia', 'honda'];
for(var element in collection2) {
print('collection2 : ${element}');
}


=>
collection2 : bmw
collection2 : hyundai
collection2 : kia
collection2 : honda


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}');
}
=>
i=0
i=1

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
}
=>
i=1
i=2

When a break is encountered in a while statement, the while statement is exited.




댓글 없음:

댓글 쓰기