ืฉืืืืืช ืืืืืืช ืืืชืจืืฉ ืืืืื ืืคืขืืช ืืชืืื ืืช. ืื ืืขืื ื ืคืืฆื ืืื ืืชืื ืช, ืืืชืืืืื ืืขื ืืงืฆืืขื ืื ืืืืชืืื. ืื ืื ืืฉืืืืืช ื ืืืขืืช ืืืฉืืช ืืืคืชื. ืืืง ืืื ืงืฉื ืืืืืช, ืืืคืขืืื ืื ืื ืืคืฉืจื. ืืืืืื, ืืืื ืืืจืืช ืชืืื ืืช, ืืืืืจ ืืจืฉืช ืขืืื ืืืคืื ืืคืชืข ืื ืฉืืืฉืื ืืืคืกืง. ืืฆืืื ืืืื ื ืงืจืืื ืืจืืืื. ื ืกื ืืชืคืืก ืื ืืืงื ืืื ืืื ืืืฉืืฉืื ืืืืคืื ืืืจืืืื.
ื'ืืืื ื ืกื ืืชืคืืก ืืืืง
ืืืฉืจ ืืชืจืืฉืช ืฉืืืื, Java ืืืจื ืืื ื ืขืฆืจืช ืืืืฆืจืช ืืืืขืช ืฉืืืื. ืชืืืื ืื ื ืงืจื "ื'ืืืื ืืืจืงืช ืืจืื". Java ืืกืคืงืช ืืชืงื ืื ืืืืืืื ืืืืคืื ืืืจืืืื. ืืื ืืื ืืื ืื ืกืืช...ืืชืคืืก...ืกืืฃ ืกืืฃ ืื ืืื. ืื ื ืืชืืืืจ ืฉื try block, catch block ืืืืกืืฃ block.//try block
try {
// Block of code to try
}
//try catch
catch(Exception e) {
// Block of code to handle errors
}
finally {
// Optional block of code
}
ืืืฉืจ ืืชืจืืฉ ืืจืื ืืืืืง ื-try, ืืฉืืืื ืขืืืจืช ืืืืืง ื-catch, ืฉืืืื ืืืชืืืื ืขื ืืืจืืื. ืื ืื ื ืืฆื ืืืืง ืืื, ืืืืขืช ืืจืืื ืื ืืืืคืืช ืชืืฆื ืืืฉืชืืฉ ืืืืฆืืข ืืืฉื ืฉื ืืชืืื ืืช ื ืขืฆืจ. ืื ืืื ืืื ืืข ืขืฆืืจืช ืืืจืื ืืื ืฉืืชื ืฆืจืื ืืืฉืชืืฉ ืืืืง try..catch. ![Java Try - Catch - 1](https://cdn.codegym.cc/images/article/ab0bf0e8-2813-4ec0-827d-164b2284a9a8/800.jpeg)
ืืงืฆืจื ืขื ืื ืกืืช, ืืชืคืืก, ืืืกืืฃ, ืืืจืง ืืืืืช ืืคืชื
ืืืคืื ืืืจืืืื ื-Java ืืืืกืก ืขื ืืฉืืืืฉ ืืืืืืช ืืืคืชื ืืืืืช ืืชืืื ืืช:- try โ ืืืืืจ ืืืฉ ืงืื ืฉืื ืืืื ืืืชืจืืฉ ืืจืื;
- catch - ืืืืืจ ืืช ืืืฉ ืืงืื ืฉืื ืืืจืื ืืืืคื;
- ืืืกืืฃ - ืืืืืจ ืืืฉ ืงืื ืฉืืื ืืืคืฆืืื ืื, ืื ืื ืงืืื, ืืืืฆืข ืืื ืืงืจื, ืืื ืงืฉืจ ืืชืืฆืืืช ืฉื ืืืืง ื-try.
- throw - ืืฉืืฉ ืืืขืืืช ืืจืื;
- throws - ืืฉืืฉ ืืืชืืืืช ืฉืืื ืืื ืืืืืืจ ืฉืืชืืื ืขืืืื ืืืจืืง ืืจืื.
ืืืืื ืงืื ืืื ืืื ืื ืืกืืื ืืชืคืืก
ื ื ืื ืฉืืฉ ืื ื ืชืืื ืืช ืขื ืืขืจื ืืืฉืื.public class TryTest {
public static void main(String[] args) {
int[] myArray = new int[5];
myArray[7] = 8;
System.out.println(myArray[7]);
}
}
ืืืืืื ืฉืื ื ืื ืกืื ืืืฉืช ืืจืืื ืืขืจื ืขื ืืื ืืงืก ืื ืงืืื, ืืชืืื ืืช ืชืฆื ืขื ืฉืืืื:
ืืจืืื ืืฉืจืฉืืจ "main" java.lang.ArrayIndexOutOfBoundsException: ืืื ืืงืก 7 ืืืืฅ ืืชืืื ืืืืจื 5 ื-days.TryTest.main(TryTest.java:6) ืืชืืืื ืืกืชืืื ืขื ืงืื ืืฆืืื 1
ืืื ื ืฉื ื ืืช ืืชืืื ืืช ืืื ืื ืืคื ืืืจืื ืื ืืขืืจืช ืชืคืืกืช ื ืืกืืื. ืงืืื ืื ื ืกื ืืืกืื, ืืืจ ืื - ืชืคืืก ืืืืง.
//try catch example
public class TryTest {
public static void main(String[] args) {
try {
int[] myArray = new int[5];
myArray[7] = 8;
System.out.println(myArray[7]);
} catch (Exception myEx) {
System.out.println("The exception was handled...");
}
System.out.println("This is the end of the program...");
}
}
ืืขืช ืืคืื ืืฉืชื ื:
ืืืจืืื ืืืคืื... ืืื ืกืืฃ ืืชืืื ืืช... ืืชืืืื ืืกืชืืื ืขื ืงืื ืืฆืืื 0 ืืชืืืื ืืกืชืืื ืขื ืงืื ืืฆืืื 0
ืืืงืจื ืื, ืืชืืื ืืช ืืืฉืืื ืืืืื, ืืืืืขื ืฉืื ื ืืืฆืืช ืขื ืืืกื. ืกืืื ื ืืื ืฉื ืืชืืื ืืช ืืกืืื ืขื ืืื ืงืื 0 ืืกืืฃ ืืชืืืื, ืืขืื ืฉืืื - 1. ืืืฉืจ ืืฉืชืืฉืื ืืืืืง try...catch, ืื ืืืฆืืจืืช ืืื ืืฆืืจืืช try ื-catch ืืืืฆืขืืช ืชืืืื. ืื ืืชืจืืฉ ืืจืื ืืืืืง try, ืืื ืคืงืืืช ืืืืฆืืข ืืจืืืื ื ืขืฆืจืช ืืืืฉืืื ืืืฆืืจืช ื-catch. ืืื, ืืืฉืจ ืืคืขืืช ืืชืืื ืืช ืืืืขื ืืืกืคืจืื[7]=8; ืฉืืจื, ืืชืืื ืืช ืชืขืฆืืจ ืืชืขืืืจ ื-catch block ืืืงืจื ืฉืื ื, ืืืจืื ื ืขื ืืืฉืชื ื myEx ืขื ืืกืื Exception. ืืืื ืืืืงืช ืืืกืืก ืืื ืืืจืืืื, ืืืื ืื ืืืืืื ืืืืืช ืฉืื ืื. ืืืืืื, ืืฉื ื ืืจืืืื ืฉืืืจืืื ืืืฆืคืช ืืืกื ืืช, ืืขืืจ ืืืื ืืงืก ืืืขืจื, ืืื ืืืงืจื ืฉืื ื, ืืฆืืืข ืขื Null ืืื'. ืื ืื ืืืื ื ืื ืืฉืื ืขื ืกืื ืืืจืืื, ืื ืื ืืชืืื ืืช ืืืืชื ืืกืชืืืืช ืืฆืืจื ืฉืืืื. ืขื ืืืช, ืืืจื ื ืืกืื Exception ืืคืฉืืืช ืืืืืื, ืืืื ืืืืืงื ืืืกืืกืืช ืืื ืืืจืืืื. ืื ืืฆืืจืช ื-catch (Exception myEx) ืชืืคื ืืืขื ืืื ืืืจืืืื. ืืืคืื ืืืจืื ืืืงืจื ืื ืืืืจ ืกืืื ืืืืง ื-catch, ืืชืืื ืืช ืืืฉืืื ืืขืืืืชื, ืืืืฆืขืช ืืช ืื ืฉืืจ ืืืืจืืืช ืืืืจ ืืืืง ื-catch. ืื ืืจืฆืื ื ืืจืืืช ืืจืืืื ืฉืืชืจืืฉื, ืชืืื ืืืงืฉ ืืืชืืื ืืช ืืืืคืืก ืขืงืืืช ืืืกื ืืช ืฉื ืงืจืืืืช ืืืชืืื. ืื ืื ืฉื-JVM ืขืืฉื ืืืฉืจ ืืชืจืืฉ ืืจืื ืฉืื ื ืชืคืก: ืืื ืขืืฆืจ ืืช ืืืฆืืข ืืชืืื ืืช, ืืืืคืืก ืขืงืืืช ืืืกื ืืช ืืืืจ ืืืฆืืข ืืงืื ืฉื ืืืืืง ืืกืืคื, ืื ืงืืื.
public class TryTest {
public static void main(String[] args) {
try {
int[] myArray = new int[5];
myArray[7] = 8;
System.out.println(myArray[7]);
} catch (Exception myEx) {
myEx.printStackTrace();
}
System.out.println("This is the end of the program...");
}
}
ืืืคืื ืืืจืื ืืืงืจื ืื ืืกืชืื ืืืืคืกืช ืขืจืืืช ืืขืงื ืืฉืืืื ืืืกืืฃ ืืืืฆืขืืช ืืฉืืื printStackTrace() ืืืืืืจืช ืืืืืงื Exception .
java.lang.ArrayIndexOutOfBoundsException: ืืื ืืงืก 7 ืืืืฅ ืืชืืื ืืืืจื 5 ื-days.TryTest.main(TryTest.java:7) ืืื ืกืืฃ ืืชืืื ืืช... ืืชืืืื ืืกืชืืื ืขื ืงืื ืืฆืืื 0
ืขื ืืืช, ืืชืืื ืืช ืืฆืื ืืจืืื.
ืกืืฃ ืื ืกืืฃ! ืืืืจ ืชืคืืกื
ืืืืืื ืืืืืืจืืช ืฉื ืืืืืช ืืืคืชื ืืืืคืื ืืืจืืืื, ืืืืจื ื ืืช ืืืืืง ืืกืืคื. ืื ืืืคืฆืืื ืื, ืืื ืื ืงืืื, ืื ืืืืฆืข ืืื ืงืฉืจ ืืชืืฆืืืช ืฉื ืืกืืืช ืื ืืกืืื. ืืืื ื ืฉื ื ืืช ืกืื ืืืจืืื ื-NullPointerException.public class TryTest {
public static void main(String[] args) {
try {
int[] myArray = new int[5];
myArray[7] = 8;
System.out.println(myArray[7]);
} catch (NullPointerException myEx) {
System.out.println("The exception was handled...");
}
finally{
System.out.println(" finally");
}
System.out.println("This is the end of the program...");
}
}
ืื ื ืืคืื:
ืืจืืื ืืฉืจืฉืืจ "main" java.lang.ArrayIndexOutOfBoundsException: ืืื ืืงืก 7 ืืืืฅ ืืชืืื ืืืืจื 5 ื-days.TryTest.main(TryTest.java:7) ืกืืฃ ืกืืฃ ืืชืืืื ืืกืชืืื ืขื ืงืื ืืฆืืื 1
ืืื, ืื ืื ื ืืืืืื ืืฆืืื ืืช ืกืื ืืืจืืื ืืืืืช ื ืืื. ืืื ืื IndexOutOfBoundsException.
public class TryTest {
public static void main(String[] args) {
try {
int[] myArray = new int[5];
myArray[7] = 8;
System.out.println(myArray[7]);
} catch (IndexOutOfBoundsException myEx) {
System.out.println("The exception was handled...");
}
finally{
System.out.println(" finally");
}
System.out.println("This is the end of the program...");
}
}
ืืืงืจื ืื, ืืคืื ืืืื ืืืืงืื:
ืืืจืืื ืืืคืื... ืืืกืืฃ ืืื ืกืืฃ ืืชืืื ืืช... ืืชืืืื ืืกืชืืื ืขื ืงืื ืืฆืืื 0
ืืื ืคืืขืืื ืืจืืืื?
ืื ืงืืื ืืื ืฉืื ืืืืืื ืืืื -ืชืคืืก, ืืจืืง, ืืจืืงืืช ืืืืืืช ืืฉืืฉ ืจืง ืขื java.lang.Throwable ืื ืฆืืฆืืื. ืืืืืื, ืืชื ืืืื ืืขืฉืืช ืืืช:public class MyClass {
public static void main(String[] args) {
try {
} catch (Throwable thr) {
}
}
}
ืขื ืืืช, ืืชื ืื ืืืื ืืขืฉืืช ืืช ืื ืืื:
public class MyClass {
public static void main(String[] args) {
try {
} catch (String thr) {
}
}
}
GO TO FULL VERSION