แสดงบทความที่มีป้ายกำกับ Java แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Java แสดงบทความทั้งหมด

วันพุธที่ 8 มีนาคม พ.ศ. 2560

Java language [ Try Catch Finally ]

ภาษาจาวามีคีย์เวิร์ด finally ที่จะมีชุดคำสั่งอยู่ในบล็อกเพื่อระบุให้โปรแกรมทำชุดคำสั่งดังกล่าวหลังจากสิ้นสุดการทำงานของชุดคำสั่งในบล็อก tryหรือ catch

ภาษาจาวาจะทำชุดคำสั่งในบล็อก finally เสมอ แม้ว่าจะมีคำสั่งreturn ในบล็อก try หรือ catch ก่อนก็ตาม กรณีเดียวที่จะไม่ทำชุดคำสั่งในบล็อก finally คือมีคำสั่ง System.exit();

  ตัวอย่างการใช้ try and catch
public class ExceptionHandlingDemoV2 {
public static void main(String args[]) {
try {
int i = Integer.parseInt(args[0]);
System.out.println(4 / i);
} catch(ArithmeticException ex) {
System.out.println(ex.toString());
} catch(NumberFormatException ex) {
System.out.println("Invalid numeric format");
}
}
}
4เราสามารถจะจัดการกับออปเจ็คของ Exception โดยใช้คลาสที่เป็น superclass ของ  Exception นั้นได้ อาทิเช่น เราสามารถจัดการกับFileNotFoundException โดยใช้ IOExceptionหรือ Exception แทนได้
4การจัดการกับ Exception มีสองแบบ คือ
•ใช้คำสั่ง try/catch
•ใช้คำสั่ง throws ในการประกาศเมธอดที่จะมีการเรียกใช้เมธอดใดๆที่อาจส่งออปเจ็คประเภท Exception

4รูปแบบการใช้คำสั่ง throws มีดังนี้
[modifier] return_type methodName([arguments]) throws
ExceptionType[,ExceptionType2] {
...
}
4ตัวอย่าง
public void openfile(String s) throwsFileNotFoundException {
...
   }

4เมธอดใดๆสามารถที่จะจัดการกับ Exception โดยใช้คำสั่งthrows ได้มากกว่าหนึ่งประเภท
4ตัวอย่าง
public void openFile(String s)throwsFileNotFoundException,UnknownHostException{
 ...
      }
4กรณีที่มีการใช้คำสั่ง throws ส่งต่อไปเรื่อยๆ แล้วเมธอด main()ซึ่งเรียกใช้เมธอดสุดท้ายที่ใช้คำสั่ง throws ไม่มีการจัดการกับออปเจ็คประเภท Exception ดังกล่าว  โปรแกรมจะเกิดข้อผิดพลาดในขั้นตอนการรันโปรแกรม เมื่อมีข้อผิดพลาดของออปเจ็คประเภท Exception ดังกล่าวเกิดขึ้น

4เมธอดแบบ overriden จะไม่อนุญาตให้มีการจัดการออปเจ็คประเภท Exception โดยใช้คำสั่ง throws มากชนิดกว่าที่เมธอดเดิมจัดการอยู่ได้

import java.io.*;
public class Parent {
  public void myMethods() throws FileNotFoundException { }
}
public class OverrideExceptionV2 extends Parent {
public void myMethods() throwsFileNotFoundException,IOException {
new FileInputStream("temp.txt");
}
}

4การสร้างคลาสประเภท Exception ขึ้นมาใหม่ สามารถทำได้โดยนิยามคลาสใดๆให้สืบทอดมาจากคลาสที่ชื่อ Exception
4โดยทั่วไปคลาสที่ชื่อ Exception จะมี constructor สองรูปแบบคือ
•public Exception()
•public Exception(String s)
4ดังนั้นคลาสที่สืบทอดมาจากคลาสที่ชื่อ Exception ควรจะมี constructor ทั้งสองแบบ โดยรูปแบบหนึ่งจะมี argument ที่มีชนิดข้อมูลเป็นString และมีคำสั่งแรกใน constructor เป็นคำสั่ง super(s);

public class MyOwnException extends Exception {
public MyOwnException (String s) {
super(s);
}
}

4เมธอดที่ต้องการส่งออปเจ็คประเภท Exception  เมื่อเกิดข้อผิดพลาดขึ้นในคำสั่งใด จะต้องเรียกใช้คำสั่งที่ชื่อ  throw  เพื่อจะสร้างออปเจ็คของคลาสประเภท Exception ขึ้นมา
4รูปแบบ
throw new ExceptionType([arguments])
4นอกจากนี้คำสั่งประกาศเมธอดนั้นจะต้องมีคำสั่ง throws เพื่อกำหนดให้คำสั่งในเมธอดอื่นๆที่เรียกใช้เมธอดนี้ต้องเขียนคำสั่งในการจัดการกับข้อผิดพลาดนี้

import java.io.*;
public class FileHandler {
    public static void openFile(String filename) throws      MyOwnException {
 File f = new File(filename);
 if (!f.exists()) {
throw new MyOwnException("File Not Found");
 }
    }
}

public class FileOpener {
 public static void main (String args[]) {
try {
FileHandler.openFile(args[0]);
System.out.println("Open successful");
} catch (MyOwnException ex) {
System.err.println(ex);
}
 }
}

Java language [ ว่าด้วยเรื่องของ mod ]

import java.io.*;
class morst 
{
 public static void main(String[] args) throws IOException
 {
  BufferedReader bfr= new BufferedReader(new InputStreamReader(System.in));
  String input,toUpper,mod="";
  int index,check;
  char chk;
  input=bfr.readLine();
  toUpper=input.toUpperCase();
  for(index=0;index<toUpper.length();index++)
   {
    //chk character
    chk=toUpper.charAt(index);
    //check integer
    check=(int)chk;
    if(check==65){mod=mod+".-";}
    if(check==66){mod=mod+"-...";}
    System.out.print(check+" "+chk);
   }
   System.out.print("Output : "+mod);
 }
}

Java language [ Loop star *]

class star 
{
 public static void main(String[] args) 
 {
  int number=Integer.parseInt(args[0]);
  if (number>=1&number<=20)
  {
   System.out.print("1-20");
  }
  else if (number>20&number<=40)
  {
   System.out.print("21-40");
  }
  else if (number>40&number<=60)
  {
   System.out.print("41-60");
  }
  else if (number>60&number<=100)
  {
   int row,colum;
   for(row=1;row<=5;row++)
    {
    for(colum=1;colum<=30;colum++)
    {
     if(row==2|row==3|row==4)
     {
      if(colum>3&colum<28)
       {
       System.out.print(" ");
      }
      else System.out.print("*");
     }
     else{
     System.out.print("*");}
     
    }
    System.out.print("\n");
   }
  }
  else System.out.print("Your intput out of rang.\nPlease Input number between 1-100.");
 }
}

บันทึกช่วงเวลาเปลี่ยนผ่าน คน กับ ai ณ ปี 2025 - 2026

 บันทึกช่วงเวลาเปลี่ยนผ่าน คน กับ ai ปีที่แล้ว 2025  Search engine & Knowledge base by ai - เปิดตัวสามารถทำหน้าที่ได้เหมือน search engi...