Java: Exception Handling Mechanism and java Exception Handling Mechanism
1. How to capture exceptions
Try
{
Abnormal code segments may occur;
}
Catch (exception type name processes the exception object)
{
Exception Handling Code segment;
}
1 import java.io.*; 2 3 public class TryCatchTest { 4 5 public static void main(String[] args) { 6 File file = new File("abc.txt"); 7 int a[] = {1, 2}; 8 9 try10 {11 System.out.println(3/0);12 }13 catch(ArithmeticException e1)14 {15 System.out.println("3/0: ");16 System.out.println("This is ArithmeticException");17 }18 19 try20 {21 System.out.println(a[2]);22 }23 catch(ArrayIndexOutOfBoundsException e2)24 {25 System.out.println("a[2] is out of Array: ");26 System.out.println("This is ArrayIndexOutOfBoundsException");27 }28 29 try30 {31 BufferedReader input = new BufferedReader(new FileReader(file));32 }33 catch (FileNotFoundException e3)34 {35 System.out.println("abc.txt is not found: ");36 System.out.println("This is FileNotFoundException");37 }38 catch(IOException e)39 {40 System.out.println("This is IOException");41 }42 43 }44 45 }
3/0:
This is ArithmeticException
A [2] is out of Array:
This is ArrayIndexOutOfBoundsException
Abc.txt is not found:
This is FileNotFoundException
2. How to throw an exception
If you do not want to capture or handle a possible exception in the code, you need to pass the exception to the method that calls it to handle the exception. In this case, throw and throws must be used.
- Throws statements are used in method declaration and an exception is thrown.
- Throws an exception when the throw statement is used inside the method body.
Note: If the throw statement is used to throw an exception, you must use the throws statement in the method declaration to declare the exception thrown in the method body, the exception thrown by the throws statement Declaration must be an exception thrown by the throw statement in the method body or the parent class of the exception.
1 import java.io.*; 2 3 public class ThrowTest { 4 5 public void throwTest1() throws ArithmeticException 6 { 7 System.out.println(3/0); 8 } 9 10 public void throwTest2() throws ArrayIndexOutOfBoundsException11 {12 int a[] ={1,2};13 System.out.println(a[2]);14 }15 16 public void throwTest3() throws FileNotFoundException17 {18 File file=new File("abc.txt");19 new BufferedReader(new FileReader(file));20 }21 22 public void throwTest4() throws FileNotFoundException23 {24 throw new FileNotFoundException("abc.txt");25 }26 27 public static void main(String[] args) {28 ThrowTest throwTest=new ThrowTest();29 30 try31 {32 throwTest.throwTest1();33 }34 catch (ArithmeticException e1)35 {36 System.out.println("3/0: ");37 System.out.println("This is ArithmeticException");38 }39 40 try 41 {42 throwTest.throwTest2();43 }44 catch(ArrayIndexOutOfBoundsException e2)45 {46 System.out.println("a[2] is out of Array: ");47 System.out.println("This is ArrayIndexOutOfBoundsException");48 }49 50 try51 {52 throwTest.throwTest3();53 }54 catch (FileNotFoundException e3)55 {56 System.out.println("abc.txt is not found: ");57 System.out.println("This is FileNotFoundException");58 }59 60 try61 {62 throwTest.throwTest4();63 }64 catch (FileNotFoundException e3)65 {66 System.out.println("abc.txt is not found: ");67 System.out.println("This is FileNotFoundException");68 }69 70 }71 72 }
3/0:
This is ArithmeticException
A [2] is out of Array:
This is ArrayIndexOutOfBoundsException
Abc.txt is not found:
This is FileNotFoundException
Abc.txt is not found:
This is FileNotFoundException
3. custom exception
To create your own Exception classes, you only need to inherit the required classes from the Exception class or from the subclass of the Exception class as needed. Traditionally, a default and constructor containing detailed information are often provided for every exception class. Note that the custom exception class must be thrown by the programmer using the throw statement.
1 public class MyException { 2 3 public static void main(String[] args) { 4 String str="2abcde"; 5 6 try 7 { 8 char c=str.charAt(0); 9 if(c<'a'||c>'z'||c<'A'||c>'Z')10 throw new FirstLetterException();11 }12 catch (FirstLetterException e)13 {14 System.out.println("This is FirstLetterException");15 }16 17 }18 19 }20 21 class FirstLetterException extends Exception{22 public FirstLetterException()23 {24 super("The first char is not a letter");25 }26 27 public FirstLetterException(String str)28 {29 super(str);30 }31 }
This is FirstLetterException
1 public class MyException { 2 3 public static void main(String[] args) throws FirstLetterException{ 4 throw new FirstLetterException(); 5 } 6 } 7 8 class FirstLetterException extends Exception{ 9 public FirstLetterException()10 {11 super("The first char is not a letter");12 }13 14 public FirstLetterException(String str)15 {16 super(str);17 }18 }
Exception in thread "main" FirstLetterException: The first char is not a letter
At MyException. main (MyException. java: 5)
4. Use finally statements
Try... the catch statement is: if an exception occurs in a try statement, all subsequent statements in the try statement segment will not be executed starting from the exception, until the end of these try statement segments.
However, in many cases, some statements need to be executed whether or not exceptions occur. You can put this part of code in the finally statement segment. Even if the try or catch statement segment contains the return Statement, the program will execute the finally statement segment after the exception is thrown, unless System is executed in the try or catch statement segment. if the exit () method or an Error occurs, the finally statement will not be executed and exit the program.
1 import java.io.*; 2 3 public class FinallyTest { 4 5 public static void main(String[] args) { 6 File file=null; 7 BufferedReader input=null; 8 file=new File("abc.txt"); 9 10 try11 {12 input=new BufferedReader(new FileReader(file));13 }14 catch(FileNotFoundException e)15 {16 System.out.print("abc.txt is not found: ");17 System.out.println("This is FileNotFoundException");18 }19 finally20 {21 System.out.println("This is finally code part.");22 }23 24 }25 26 }
Abc.txt is not found: This is FileNotFoundException
This is finally code part.