java--Exception Handling

Source: Internet
Author: User

exception

<1> Runtime Error (not a compile-time syntax error). <2> observing the wrong name and line number is important!  "program analysis: Array out of Bounds"int [] arr = { A-i};System.out.println (arr[4]);Note: Compiled in C and C + + because the array is not checked out of bounds in C and C + + (this can easily cause a buffer overflow vulnerability). Java can check that the array subscript is out of bounds, but there is a certain range of checks. Error : Exception in thread ' main ' java.lang.arrayindexoutofboundsexception:4[error subscript]at * * * * (Error location)----array subscript out of bounds "Program Analysis: Denominator is 0"System.out.println (2/0);error: Exception in thread ' main ' java.lang.ArithmaticException:by zero at * * *FIX: Catch exceptiontry{System.out.println (2/0);}catch (arithmaticexception ae) {//java Program in error, AE object can be used directly, within the default is initializedSystem.out.println ("System maintenance ..."); //This is a common hint for many websites, so continuous refresh does not solve the problemAe.printstacktrace (); //This is a common workaround, which is to print out the wrong stack information (print all the information that caused the error so that the programmer is ready to debug)  }   "program" <1> View API documentation It is known that each method will have a corresponding exception, intended to remind the user to use this method to pay special attention to the occurrence of the exception, you can use the throw exception statement to solve, and an exception is directly in the method declaration is given, For example, public int read () throws ioexception{}. <2> Use the throws keyword to throw a possible exception when the method is defined, or use the Try-catch statement to catch an exception when the method is called. usage of <3>throw:void A (int i) throws arithmaticexception{if (i==0) {throw new Arithmaticexception ("Dividend cannot be 0");//This is equivalent to a false print statement               }         }  "Exception Classification"  <1> All exceptions are from such a class: Throwable (Java.lang.Throwable has two subclasses--error, Exception). <2>exception is an exception that can be initialized, and there is a special exception: RuntimeException. <3>throwable means "can be thrown", as long as an object that inherits from that class can throw an exception. <4>error is a system-level error, that is, a virtual machine error, which our users cannot handle; exception can be handled by the user. <5>runtimeexception is a recurring error that can be handled without processing Another type of exception (not the runtime's exception) must be captured and processed (there are some methods in the JDK that directly write throws that represent the exception that must be handled).   "Exception category" 
"Finally"  working principle: the necessity of existence:If an open file operation is performed earlier, the corresponding code will not continue to execute when an error occurs, but we must close the open file, and this code will need to be placed in the finally code block.  "Try"  "Catch"  "Finally"  Sample program:  "throwing and handling of exceptions" Program:void F () throws filenotfoundexception,ioexception{//More rigorous is to throw a variety of possible exceptions, can be based on the actual caught exception to make different processingFileInputStream in = new FileInputStream ("**.txt");int b;B = In.read ();While (b = =-1) {System.out.println ((char) b);B = In.read ();      }  } void F2 () {try{f ();catch (FileNotFoundException e) {e.getmessage ();//The exception must be dealt with accordingly, otherwise it would be equivalent to exception thought to have annexed}catch (IOException e) {e.printstacktrace ();         }     }} void F2 () throws ioexception{f ();  }//Catch an exception and do not know how to handle it, you can use the above method. Note: If any exceptions do not want to be handled, you can also throw an exception directly on the main function, when the exception thrown is given to the Java runtime System. In addition to doing the test unexpectedly, do not throw an exception in the main function in the actual programming.    "exception Example" Note: It is important to pay particular attention to the line number of the error (which is of paramount importance-The fault in which the method was found), which is often caused by one of the local errors.   Note: Exception should be placed in the back and not in front of its subclasses. Error: The exception was caught java.io.FileNotFoundException * * * *  "Custom Exception" in actual projects, it is often necessary to define the required exception classes, inheriting from existing classes (runtimeexception, etc.).  steps: "Example"   "Attention" note:<1> The first thrown exception type and the original inconsistency should be ioexception. <2> The second throws a large range (the range is too big to be small). <3> The third non-throwing exception can be overridden by a method. <4> Fourth throws an exception much more.                                                                                          

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.