Java starts from scratch and the basics of learning notes & lt; Exception & gt; (15th)

Source: Internet
Author: User

Exception: it refers to a programmer's problems caused by programming errors or general operations. It can be solved or avoided in case of exceptions. Error: the programmer cannot solve the problem by modifying the code. General errors such as memory overflow and JVM errors are errors. Exception category: runtime exception and compilation exception example: public class ExceptionDemo01 {publicstatic void main (String [] args) {int I = 12; int j = 0; int m = I/j; System. out. println (m) ;}} the following exceptions occur during running: Exception in thread "main" java. lang. arithmeticException:/by zero at com. ibm. exceptions. exceptionDemo01.main (ExceptionDemo01.java: 7) The program has an exception. The exception prompt is that the number of divisor cannot be 0 String k = "%"; int l = Integer. parseInt (k); System. out. println (l + 1); Ti On in thread "main" java. lang. numberFormatException: For input string: "%" at java. lang. numberFormatException. forInputString (Unknown Source) at java. lang. integer. parseInt (Unknown Source) at java. lang. integer. parseInt (Unknown Source) at com. ibm. exceptions. predictiondemo01.main (ExceptionDemo01.java: 11): Numeric formatting exception Common exceptions: v RuntimeException § ArithmeticException: mathematical calculation exception § NullPointerException: NULL pointer exception § Ne GativeArraySizeException: Negative array length exception § unknown: array index out-of-bounds exception § ClassNotFoundException: class file not found exception § ClassCastException: Shape exception v IOException § FileNotFoundException: file not found exception § EOFException: an exception occurs at the end of a read/write file. § MalformedURLException: URL format error. § SocketException: The processing mechanism of a Socket exception. If a program is running, when it is running to a code that may have an exception, an exception class object will be generated, and the Throw will Throw the object to the outside. The catch Block will capture the exception object and then the content in the catch block will be executed accordingly, if the catch Block can handle this exception, it will be processed directly. If it cannot, the program will stop. Programmers can only handle exceptions and cannot handle errors. Common Exception Handling statement Try {} catch (exception 1 Object 1) {// If exception 1 is thrown, execute the content in this catch block} catch (exception 2 object 2) {// If exception 2 is thrown, execute the content in the catch Block} catch (exception 3 object 3) {// if an exception is thrown 3, execute the content in the catch Block} finally {// the content to be executed whether you throw an exception or do not throw an exception} use the throws keyword to throw an exception package. com. ibm. exceptions; import java. io. EOFException; import java. io. fileInputStream; import java. io. fileNotFoundException; public class ExceptionDemo02 {public void show () throws FileNotFoundException, EOFEx Ception {// This line of code may report an exception. There are two Processing Methods: // 1. Use try {} catch () directly here () {} block capture exception // 2 throws an exception using the throws keyword. throws is written after the method name, // What is the difference between the class name of one or more exception classes after Throws? // What is the difference between throw and throws? Throw is followed by an exception class object. throws is followed by one or more exception classes, FileInputStream (new FileInputStream ("c: \ aa.txt ");} public static void main (String [] args) {// call the show method to handle the exception thrown out by the show method. try {ExceptionDemo02 ed02 = new ExceptionDemo02 (); ed02.show ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (EOFException e) {e. printStackTrace () ;}} v can throw an exception in the method definition in the following cases: § call a method that throws "Checked exceptions". § an error occurs during the program running and Throw a "Checked Exception" public void show3 () throws Exception {// throw Exception throw new Exception () using the throw clause;} v does not throw the following Exception: § AnnotationFormatError, AssertionError, AWTError, warn, FactoryConfigurationError, cause, IOError, LinkageError, ServiceConfigurationError, ThreadDeath, warn, VirtualMachineError derived from RuntimeException, such as Null PointerException and so on. If an exception is not processed in the current try-catch module, it will throw it to its call method. V if an exception returns to the main () method and is still not processed, the program will terminate the exception. Public class ExceptionDemo03 {public void show () throws FileNotFoundException {fileinputstreamisi = newFileInputStream ("C: \ AA. TXT ");} publicstatic void main (String [] args) throws FileNotFoundException {new ExceptionDemo03 (). show () ;}} capture and throw exceptions in combination with public class ExceptionDemo03 {public void show () throws FileNotFoundException {FileInputStream FS = new FileInputStream ("C: \ AA. TXT ");} publicstatic vo Id main (String [] args) {try {new ExceptionDemo03 (). show ();} catch (FileNotFoundException e) {e. exception in printStackTrace () ;}} method overwrite: package com. ibm. exceptions; import java. io. IOException; public class FatherException {public void show () throws IOException {System. out. println ("f") ;}} package com. ibm. exceptions; import java. io. fileNotFoundException; import java. io. IOException; public class SonExc Eptionextends FatherException {// when the method in the parent class needs to be overwritten in the subclass // if the method in the parent class throws an exception, however, the method with the same name in the subclass does not throw an exception // at this time, the method in the subclass does not overwrite the method in the parent class // If the subclass wants to overwrite the method that throws an exception in the parent class, the method with the same name in the subclass also needs to throw an exception // The exception class thrown by throws in the subclass method is either the same as the exception class in the parent class or the public void of the exception class thrown by the parent class method. show () throws IOException, FileNotFoundException {System. out. println ("s");} public static void main (String [] args) {FatherException fe = new SonException (); try {fe. show ();} catch (IOExcepti On e) {e. printStackTrace () ;}} custom Exception: You define an Exception class. Each custom Exception class must inherit an Exception class. Generally, the Exception class is inherited, however, if we create a custom Exception class, we need to write the same constructor as the Exception class in the custom Exception class, and call the parent constructor package com. ibm. exceptions; public classMyException extends Exception {public MyException () {// call the super ();} public MyException (String message) {super (message );} public MyException (String message, Throwable cause) {super (message, cause) ;} Public MyException (Throwable cause) {super (cause) ;}} package com. ibm. exceptions; public class MyExceptionTest {public staticvoid main (String [] args) {try {throw new MyException ("throw MyException! ") ;}Catch (MyException e) {e. printStackTrace ();}}}

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.