10. ตัว อย่ำ งโปรแกรมที่ม ีก ำรจัด กำร
กับ ข้อ ผิด พลำด
public class ExceptionHandlingDemo {
public class ExceptionHandlingDemo {
public static void main(String args[]) {
public static void main(String args[]) {
try {
try {
System.out.println(args[2]);
System.out.println(args[2]);
} catch(ArrayIndexOutOfBoundsException ex) {
} catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("There is no third command
System.out.println("There is no third command
line argument");
line argument");
}}
}}
}}
12. ตัว อย่ำ งโปรแกรม
public class ExceptionHandlingDemoV2 {
public class ExceptionHandlingDemoV2 {
public static void main(String args[]) {
public static void main(String args[]) {
try {
try {
int i = Integer.parseInt(args[0]);
int i = Integer.parseInt(args[0]);
System.out.println(4 / i);
System.out.println(4 / i);
} catch(ArithmeticException ex) {
} catch(ArithmeticException ex) {
System.out.println(ex.toString());
System.out.println(ex.toString());
} catch(NumberFormatException ex) {
} catch(NumberFormatException ex) {
System.out.println("Invalid numeric format");
System.out.println("Invalid numeric format");
}}
}}
}}
13. ตัว อย่ำ งโปรแกรมที่ไ ม่ถ ูก ต้อ ง
public class ExceptionHandlingDemoV3 {
public class ExceptionHandlingDemoV3 {
public static void main(String args[]) {
public static void main(String args[]) {
try {
try {
int i = Integer.parseInt(args[0]);
int i = Integer.parseInt(args[0]);
System.out.println(4 / i);
System.out.println(4 / i);
System.out.println(args[2]);
System.out.println(args[2]);
} catch(RuntimeException ex) {
} catch(RuntimeException ex) {
System.out.println(ex.toString());
System.out.println(ex.toString());
} catch(ArrayIndexOutOfBoundsException ex) {
} catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("There is no third command
System.out.println("There is no third command
line argument");
line argument");
}}
}}
}}
15. ตัว อย่ำ งโปรแกรม
public class FinallyDemo {
public class FinallyDemo {
public static void main(String args[]) {
public static void main(String args[]) {
try {
try {
System.out.println(args[2]);
System.out.println(args[2]);
System.out.println("Hello");
System.out.println("Hello");
} catch(ArrayIndexOutOfBoundsException ex) {
} catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("There is no third argument");
System.out.println("There is no third argument");
} finally {
} finally {
System.out.println("Finish running the program");
System.out.println("Finish running the program");
}}
}}
}}
16. ตัว อย่ำ งโปรแกรม
public class FinallyDemoV2 {{
public class FinallyDemoV2
public static void main(String args[]) {{
public static void main(String args[])
FinallyDemoV2 obj == new FinallyDemoV2();
FinallyDemoV2 obj new FinallyDemoV2();
obj.myMethod(args);
obj.myMethod(args);
}}
public int myMethod(String args[]) {{
public int myMethod(String args[])
try {{
try
System.out.println(args[2]);
System.out.println(args[2]);
return 0;
return 0;
}} catch(ArrayIndexOutOfBoundsException ex) {{
catch(ArrayIndexOutOfBoundsException ex)
System.out.println("There is no third argument");
System.out.println("There is no third argument");
}} finally {{
finally
System.out.println("Finish running the program");
System.out.println("Finish running the program");
return 1;
return 1;
}}
}}
}}
22. ตัว อย่า งโปรแกรม ที่ม ีเ มธอดแบบ
overriden ทีถ ูก ต้อ ง
่
import java.io.*;
import java.io.*;
public class Parent {
public class Parent {
public void myMethods() throws IOException { }
public void myMethods() throws IOException { }
}}
___________________________________________________________
___________________________________________________________
public class OverrideException extends Parent{
public class OverrideException extends Parent{
public void myMethods() throws IOException {
public void myMethods() throws IOException {
new FileInputStream("temp.txt");
new FileInputStream("temp.txt");
}}
}}
23. ตัว อย่า งโปรแกรม ที่ม ีเ มธอดแบบ
overriden ที่ไ ม่ถ ูก ต้อ ง
import java.io.*;
import java.io.*;
public class Parent {
public class Parent {
public void myMethods() throws FileNotFoundException { }
public void myMethods() throws FileNotFoundException { }
}}
__________________________________
__________________________________
public class OverrideExceptionV2 extends Parent {
public class OverrideExceptionV2 extends Parent {
public void myMethods() throws
public void myMethods() throws
FileNotFoundException,IOException {
FileNotFoundException,IOException {
new FileInputStream("temp.txt");
new FileInputStream("temp.txt");
}}
}}
27. ตัว อย่า ง คลาส FileHandler
import java.io.*;
import java.io.*;
public class FileHandler {
public class FileHandler {
public static void openFile(String filename) throws
public static void openFile(String filename) throws
MyOwnException {
MyOwnException {
File f = new File(filename);
File f = new File(filename);
if (!f.exists()) {
if (!f.exists()) {
throw new MyOwnException("File Not Found");
throw new MyOwnException("File Not Found");
}}
}}
}}
28. ตัว อย่า งโปรแกรมที่ม ีก ารจัด การ
กับ ข้อ ผิด พลาด
public class FileOpener {
public class FileOpener {
public static void main (String args[]) {
public static void main (String args[]) {
try {
try {
FileHandler.openFile(args[0]);
FileHandler.openFile(args[0]);
System.out.println("Open successful");
System.out.println("Open successful");
} catch (MyOwnException ex) {
} catch (MyOwnException ex) {
System.err.println(ex);
System.err.println(ex);
}}
}}
}}