/** * @authorAdministrator * Function: Exception*/ Packagecom.test;ImportJava.io.*;Importjava.net.*; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//Check Exception compiler implementation can judge the exception 1. Open File//What if I can't find it?//FileReader fr = new FileReader ("D:\\aa.txt"); //2. Connect the port number of a 192.168.12.12 IP 4567//cmd under tracert tracking router//cmd to view the route table route or route print//cmd under netstat-an command to see who's practicing.//2. Connect the port number of a 192.168.12.12 IP 4567//Unknown host Check exception//socket s = new socket ("192.168.1.123", 78); //Run Exception//except 0//int a = 4/0; //int b = 6/0; //array out of bounds exception//int arr[] = {n/a}; //System.out.println (arr[234]); //External Environment Exception//What is the difference between an exception and an error//Try catch capture processing or throwing exceptions let's call your previous layer to handle//Exception Handling//try {//FileReader fr = new FileReader ("D:\\aa.txt");// //} catch (Exception e) {// //catch (Exception e) for maximum capture// //Todo:handle Exception// //Export the abnormal information to eliminate the bug//e.printstacktrace ();// } //first enter the entry block of the exception that should be caught.FileReader FR =NULL; Try{FR=NewFileReader ("D:\\bb.txt"); System.out.println ("Go On"); Socket s=NewSocket ("192.168.1.123", 78); //The following does not execute, terminates the execution code where the exception occurred, and then goes to the catch statement//If you have multiple catch statements, then the catch block that goes into the matching exception executes}Catch(FileNotFoundException E1) {//What is considered an anomaly catches what anomaly, when minimum capture, FileNotFoundException for minimum capture//Todo:handle Exception//Export the abnormal information to eliminate the bugSYSTEM.OUT.PRINTLN ("Test"); E1.printstacktrace (); //processing}Catch(IOException E2) {//Todo:handle ExceptionE2.printstacktrace (); //System.out.println ("message=" +e2.getmessage ()); //after executing the system exception exit, finally will not be executed//System.exit (-1); //processing}finally{//finally blocks are bound to be executed .System.out.println ("Enter finally"); //this block of statements, whether or not an exception, will be executed//generally speaking, put the resource that needs to be closed, [file, connect, memory] if(FR! =NULL)//previously defined when NULL is not equal to null means open { //You can also throw an exception when you close a file close first Try{fr.close (); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }}} System.out.println ("OK"); }}
Try-catch-finally of Java exception handling