ram2 ๐Ÿš—

[Flutter] Exceptions ์ด๋ž€!? ๋ณธ๋ฌธ

๐Ÿ’ง flutter

[Flutter] Exceptions ์ด๋ž€!?

coram22 2022. 7. 6. 00:39
728x90
๋ฐ˜์‘ํ˜•

Exceptions

์˜๋ฏธ: exceptions๋Š” ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ผ์ด ๋ฐœ์ƒํ–ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ค๋ฅ˜์ด๋‹ค.
๊ธฐ๋Šฅ: dart code๋Š” exceptions๋ฅผ throw ํ•˜๊ณ  catchํ•  ์ˆ˜ ์žˆ๋‹ค.
ํŠน์ง•: dart๋Š” exception๊ณผ error types๋ฅผ ์ œ๊ณตํ•˜์ง€๋งŒ, null์ด ์•„๋‹Œ ๊ฐ์ฒด๋งŒ throw ํ•  ์ˆ˜ ์žˆ๋‹ค.

exceptions๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์•Œ์•„์•ผ ํ•˜๋Š” ์šฉ์–ด

throw

exception๋ฐœ์ƒ ์‹œ ๊ฐ์ฒด ๋˜๋Š” ์ฝ”๋“œ ๋“ฑ์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
//์ผ๋‹จ ์—๋Ÿฌ๋ฅผ ๋˜์ ธ๋ณธ๋‹ค๊ณ  ์ƒ๊ฐ

throw ์‚ฌ์šฉ ์ฝ”๋“œ ์˜ˆ)

//example 1
throw FormatException('Expected at least 1 section');
//example 2
throw 'Out of llamas!';

example 1 ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค.

  • ์—๋Ÿฌ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ด๋Š” implemeter ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ถŒ์žฅ.
    why?
    implemeter์— ๋‹ค์–‘ํ•œ exception๊ณผ error์˜ ์ข…๋ฅ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ.

catch

catch๋Š” ๋‹ค์–‘ํ•œ exception์ƒํ™ฉ์„ ์ œ์‹œํ•ด์„œ ํŠน์ • ์ƒํ™ฉ์˜ ์—๋Ÿฌ ์ƒํ™ฉ์„ ์ œ์–ดํ•œ๋‹ค.
-> ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š” ํ•œ ์˜ˆ์™ธ๊ฐ€ ์ „ํŒŒ๋˜์ง€ ์•Š๋Š”๋‹ค.
//exception์„ catch ํ•œ๋‹ค๊ณ  ์ดํ•ดํ•จ

catch๋ฌธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์“ฐ๋ฉด ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์—๋Ÿฌ ์ƒํ™ฉ ์ œ์–ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์˜ค๋ฅ˜ ์ƒํ™ฉ ์•ž์— on์„ ์“ฐ๊ฑฐ๋‚˜, catch๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

<์˜ˆ์‹œ์ฝ”๋“œ1>

try {
  breedMoreLlamas();
} on OutOfLlamasException {
  buyMoreLlamas();
}
//OutofLiamasException์ด๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋ฉด buyMoreLlamas()์‹คํ–‰.

<์˜ˆ์‹œ์ฝ”๋“œ2>

try {
  breedMoreLlamas();
} on OutOfLlamasException {
  // ์ง€์ •ํ•ด๋‘” ํŠน์ • ์˜ค๋ฅ˜ ์ƒํ™ฉ(OutOfLlamas๋ผ๋Š” ์˜ค๋ฅ˜ ์ƒํ™ฉ) ์ฒ˜๋ฆฌ
  buyMoreLlamas();
} on Exception catch (e) {
  // exception ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•œ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ 
  // (์œ„์— ์„ ์–ธํ•œ OutOfLlamasException๋Š” ์ œ์™ธ)
  print('Unknown exception: $e');
} catch (e) {
  // ํƒ€์ž…์ด ํŠน์ •ํ•˜๊ฒŒ ์ง€์ •๋˜์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ
  print('Something really unknown: $e');
}
//ํ•œ ๊ฒฝ์šฐ์”ฉ ์œ„์—์„œ๋ถ€ํ„ฐ ๊ฑธ๋Ÿฌ์„œ ๋‚ด๋ ค์˜จ๋‹ค๊ณ  ์ƒ๊ฐ

rethrow

rethrow๋Š” ์˜ค๋ฅ˜๋ฅผ ์žฌ์ „ํŒŒํ•œ๋‹ค. ํ•จ์ˆ˜์˜ ์˜ค๋ฅ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์žฌ์ „ํŒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์˜ˆ์™ธ๋ฅผ ์™„์ „ํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

<์˜ˆ์‹œ์ฝ”๋“œ>

void misbehave() {
  try {
    dynamic foo = true;
    print(foo++); // ๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค๋ฉด
  } catch (e) { // ์—ฌ๊ธฐ์„œ ์˜ค๋ฅ˜๋ฅผ catchํ•˜๊ณ ,
    print('misbehave() partially handled ${e.runtimeType}.');
    rethrow; // callํ•œ ํ•จ์ˆ˜(ํ•ด๋‹น ์˜ˆ์‹œ์—์„œ๋Š” mainํ•จ์ˆ˜)๊ฐ€ ์ด ์˜ค๋ฅ˜ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์žฌ์ „ํŒŒ
  }
}

void main() {
  try {
    misbehave(); // rethrow๋กœ ์œ„์—์„œ ์ฐพ์€ ์—๋Ÿฌ๊ฐ€ ์žฌ์ „ํŒŒ๋˜์–ด ๋ฐœ์ƒ.
  } catch (e) { // ์—๋Ÿฌ๋ฅผ catch
    print('main() finished handling ${e.runtimeType}.');
  }
}

finally

finally๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ์˜ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„ ์—†์ด ์ผ๋ถ€ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

finally๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ์œผ๋กœ ๋‚˜๋‰œ๋‹ค.

1. catch๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ

์—๋Ÿฌ ์ƒํ™ฉ์— ์•Œ๋งž๋Š” catch๋ฌธ์ด ์žˆ์œผ๋ฉด, catch๋ฌธ์„ ๋จผ์ € ์‹คํ–‰ํ•˜๊ณ  finally ์‹คํ–‰
์‹คํ–‰ ์ˆœ์„œ: catch๋ฌธ -> finally

<์˜ˆ์‹œ์ฝ”๋“œ>

try {
  breedMoreLlamas();
} catch (e) {
  print('Error: $e'); // ์—๋Ÿฌ๋ฅผ ๋จผ์ € ์ œ์–ดํ•œ๋‹ค.
} finally {
  cleanLlamaStalls(); // ๊ทธ ๋‹ค์Œ, ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
}

2. catch๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ

์—๋Ÿฌ ์ƒํ™ฉ์— ์•Œ๋งž๋Š” catch๋ฌธ์ด ์—†์œผ๋ฉด exception์ด ์žˆ์–ด๋„ finally๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
์‹คํ–‰ ์ˆœ์„œ: finally -> exception

<์˜ˆ์‹œ์ฝ”๋“œ>

try {
  breedMoreLlamas();
} finally {
    // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ์•„๋ž˜ ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ ์‹คํ–‰
  cleanLlamaStalls();
  //catch๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ์žฌ์ „ํŒŒ ๋œ๋‹ค.
}

exception code example

typedef VoidFunction = void Function();

class ExceptionWithMessage {
  final String message;
  const ExceptionWithMessage(this.message);
}

// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
  void logException(Type t, [String? msg]);
  void doneLogging();
}

void tryFunction(VoidFunction untrustworthy, Logger logger) {
  // Invoking this method might cause an exception. Catch and handle
  // them using try-on-catch-finally.
  untrustworthy();
}

solution code

typedef VoidFunction = void Function();

class ExceptionWithMessage {
  final String message;
  const ExceptionWithMessage(this.message);
}
// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
  void logException(Type t, [String? msg]);
  void doneLogging();
}

void tryFunction(VoidFunction untrustworthy, Logger logger) {
// Invoking this method might cause an exception. Catch and handle
    // them using try-on-catch-finally.
  try {
    untrustworthy();
  } on ExceptionWithMessage catch (e) {
    logger.logException(e.runtimeType, e.message);
  } on Exception {
    logger.logException(Exception);
  } finally {
    logger.doneLogging();
  }
}

์ฐธ๊ณ  ์‚ฌ์ดํŠธ1: https://m.blog.naver.com/mingdyuo/221803704762
์ฐธ๊ณ  ์‚ฌ์ดํŠธ2: https://dart.dev/codelabs/dart-cheatsheet#initializer-lists

728x90
๋ฐ˜์‘ํ˜•