Java: Exception Handling Mechanism

Source: Internet
Author: User

Java: Exception Handling Mechanism
1. how to catch an exception try {code segment that may cause exceptions;} catch (exception type name handles this 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 Sy Stem. 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 FileNotF OundException "); 37} 38 catch (IOException e) 39 {40 System. out. println ("This is IOException"); 41} 42 43} 44 45} 3/0: This is arithmetic1_tiona [2] is out of Array: This is ArrayIndexOutOfBoundsExceptionabc.txt is not found: this is FileNotFoundException 2. if you do not want to capture or handle a possible exception in the code, you need to pass the exception, the method passed to call it to handle the exception. In this case, throw and throws must be used in the method declaration. throw an exception throw is used inside the method body. Note the following when throwing an exception: if the throw statement is used in the method body to throw an exception, you must use the throws statement in the method declaration to declare the exception thrown in the method body. At the same time, 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. th RowTest2 (); 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. throwTe St4 (); 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 arithmetic1_tiona [2] is out of Array: This is ArrayIndexOutOfBoundsExceptionabc.txt is not found: this is FileNotFoundExceptionabc.txt is not found: This is FileNotFoundException 3. create your own exception class for custom exceptions. All you need to do is Or inherit the required class from the subclass of the Exception class. 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 = "2 abcde"; 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 c Har 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 letterat MyException. main (MyException. java: 5) 4. use the finally statement in 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 FileNotFoundExceptionThis is finally code part.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.