สมัครเรียนโทร. 085-350-7540 , 084-88-00-255 , ntprintf@gmail.com

Tutorial DART

L01 DART INTRO L02 DART HOWTO L03 DART GETTING START L04 DART SYNTAX L05 DART VARIABLE 01 L06 DART VARIABLE 02 L07 DART FUNCTION L08 DART OPTIONAL NAMED POSITONAL L09 DART LIST L10 DART CLASS L11 DART INHERITANCE MIXIN L12 DART EXCEPTION L13 DART GENERATOR AND ITERATOR L14 DART OPERATION

Exception ในภาษา Dart

เรียนเขียนโปรแกรมง่ายๆกับ Expert Programming Tutor ในบท Dart Exception 
           ในบทนีเราจะพูดถึงหัวข้อเรื่อง exception เวลาเราทำการเขียนโปรแกรม ถ้ามันเกิด error ก็คือตัวโปรเเกรมเราก็จะทำการหยุดการทำงาน เราสามารถเลี่ยงการหยุดการทำงานตรงนั้นได้ โดยการครอบด้วยบล็อคด้วย try() catch ก็จะคุ้นเคยมาจากภาษา java c# มาบ้าง เราก็สามารถเอามาใช้ในภาษา dart ได้เหมือนกัน เบื้องต้น try ก็คือ ทำ,ก่อน ถ้ามันเกิด มันก็จะเขาไปที่ catch ซึ่งในที่นี้คือเราก็จะมาพารามิเตอร์ 2 ตัว อันดับเเรกก็คือ ตัวแปรerror นั่นเอง ต่อมาก็คือ ตัวแปรstack ครับ สุดท้ายเเล้วตัวบอก finally ก็คือบอกสุดท้ายเลย ไม่ว่าเราจะเข้า try catch ยังไงมันก็ต้องจบที่ finally 
ตัวอย่าง
จะเกิดไรขึ้นถ้าเรา ใส่ ++

void main() {
  // Excaptions
  //https://api.dart.dev/stable/2.8.0/dart-core/dart-core-library.html

    try {
    int aaa; 
    aaa++;
  } catch (e, s){
    print('error: $e \n');

    print('stack: $s \n');
  } finally {
    print('finally \n');
  }  
}


เนื่องจาก none อยู่ดีๆมา + มันก็ไม่รู้ว่ามันจะตัองทำอะไร  ต่อมามาดูที่  print stack มันขึ้นไม่สามารถอ่านคุณสมบัติได้จากค่า none เนื่องจากมันมี ++ อยู่เเต่ถ้าเราใส่ 1 มันจะเป็นอย่างนี้

void main() {
  // Excaptions

  //https://api.dart.dev/stable/2.8.0/dart-core/dart-core-library.html  

  try {
    int aaa; 
    aaa++;

  } catch (e, s){
    print('error: $e \n');
    print('stack: $s \n');
  } finally {
    print('finally \n');

  }  
}

อย่างที่บอก เราสามารถดักจับได้ ถ้ามันเกิดerror ใน try มันจะมี error หลายอย่างเช่น ใน methoed มันไม่ซัพพอร์ต เรื่องของ arrays ดังตัวอย่าง

void main() {
  // Excaptions
  //https://api.dart.dev/stable/2.8.0/dart-core/dart-core-library.html

  try {
    int aaa; 
    aaa++;
    var bbb = [2,3];
    bbb[9];
  } catch (e, s){
    print('error: $e \n');
    print('stack: $s \n');
  } finally {
    print('finally \n');
  }  
}

อยู่ดีๆเราจะมา access เข้า index ที่เป็นตัวที่ 9 เนี่ย มันไม่ได้ มันก็จะขึ้น catch เดี๋ยวมาดูกัน ขอ comment บรรทัดนี่ไว้จะได้เห็นภาพ

try {
   // int aaa = 1;
  //  aaa++;

เนื่องจากที่เราเข้า index มันมากกว่า 2 เราสามารถดักจับได้ เเต่บางที error มันเยอะไง เช่นเรามีerror 2 ตัว จะยกตัวอย่างให้ดู เราสามารถทำอย่างนี้ได้คือ  เข้าด้วยบล็อก on คือการ print เพื่อดักข้อผิดพลาด  

void main() {
 // Excaptions
  //https://api.dart.dev/stable/2.8.0/dart-core/dart-core-library.html
try {
    int aaa = 1;
    aaa++;
    var bbb = [2,3];
    bbb[9];
     }on NoSuchMethodError{
    print('NoSuchMethodError');
  }on RangeError{
    print('RangeError') ; 
  } catch (e, s){
    print('error: $e \n');
    print('stack: $s \n');
  } finally {
    print('finally \n');
  }  
}

มันจะเกิด exception ที่ตัว RangeError มันเลยตายที่บรรทัดนี้ก่อน ในชีวิตจริงเราอาจจะไม่เกิดError  อันนี้ก็คือการควบคุมเเละดักข้อผิดพลาดของมัน เพื่อตรวจสอบได้เร็ว เราจะดูเพิ่มเติมได้จากที่ไหน เราจะแปะลิ้งค์ไว้ด้านล่าง คือ การ excaption https://api.dart.dev/stable/2.8.0/dart-core/dart-core-library.html



บทความนี้อาจจะมีที่ผิด กรุณาตรวจสอบก่อนใช้

หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor

ไม่อยากอ่าน Tutorial อยากมาเรียนเลยทำอย่างไร?

สมัครเรียน ONLINE ได้ทันทีที่ https://elearn.expert-programming-tutor.com

หรือติดต่อ

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM

แผนที่ ที่ตั้งของอาคารของเรา

C Article


C++ Article


Java Article


C#.NET Article


VB.NET Article


Python Article


Golang Article


JavaScript Article


Perl Article


Lua Article


Rust Article


Article


Python


Python Numpy


Python Machine Learning



แผนผังการเรียนเขียนโปรแกรม

Link อื่นๆ

Allow sites to save and read cookie data.
Cookies are small pieces of data created by sites you visit. They make your online experience easier by saving browsing information. We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Copyright (c) 2013 expert-programming-tutor.com. All rights reserved. | 085-350-7540 | 084-88-00-255 | ntprintf@gmail.com

ติดต่อเราได้ที่

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM
แผนที่ ที่ตั้งของอาคารของเรา