Try-catch-finally of Java exception handling

Source: Internet
Author: User
Tags try catch

/** * @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

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.