Java Learning--The handling of abnormal exception

Source: Internet
Author: User
Tags getmessage

* Second, how to deal with exception anomalies
* Java provides a catch-and-throw model of exception handling
* *. " Throw ": When we execute the code, when an exception occurs, an object of the corresponding exception type is generated at the code of the exception and
* throws this object. (Auto throw/manual throw)
* > Once the object of this exception class is thrown, the program terminates execution
* > The object of this exception class is thrown to the caller of the method.
* 2. " Catch ": Catch the object of the exception class thrown out of the previous step. How to catch? That is, the way the exception is handled
* Java provides two ways to handle an object of an exception class.
* The way of processing one:
* try{
*//code that may appear abnormal
*}catch (Exception1 E1) {
*//Way of handling 1
*}catch (Exception2 E2) {
*//Way of handling 2
*}finally{
*//code that must be executed
 *   }
* Note: variables declared in 1.try, similar to local variables, out of the try{} statement, cannot be called
* 2.finally is optional.
* Inside the 3.catch statement is the handling of the exception object:
* >getmessage (); Printstacktrace ();
* 4. There can be multiple catch statements, and the exception class object thrown in the try matches the type of the exception class in the catch, once it satisfies
* Executes the code in the catch. After execution, jump out of multiple catch statements
* 5. If the exception is handled, then the subsequent code continues to execute.
* 6. If multiple exception types in a catch are "side-by-side" relationships, you can either.
* If more than one exception type in a catch is a "containment" relationship, the subclass must be placed above the parent class for processing. otherwise error!
* 7.finally is stored in code that is bound to be executed, regardless of whether the exception is still unhandled in the try, catch, and if there is a return statement.
* 8.try-catch can be nested.
 *   
* Third, for run-time exceptions, can not be explicitly processed.
* For compile-time exceptions, you must explicitly handle them.
1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 Importjava.io.FileNotFoundException;4 Importjava.io.IOException;5 Importjava.util.Date;6 Importjava.util.InputMismatchException;7 ImportJava.util.Scanner;8 9 Importorg.junit.Test;Ten  One  A  Public classTestException1 { -     //compile-time exceptions - @Test the      Public voidTest6 () { -FileInputStream FIS =NULL; -         Try { -FIS =NewFileInputStream (NewFile ("Hello.txt")); +             intb; -              while((b = Fis.read ())! =-1) { +System.out.print ((Char) b); A             } at  -}Catch(FileNotFoundException E1) { -System.out.println ("The file can't be found!") "); -}Catch(IOException E1) { - System.out.println (E1.getmessage ()); -}finally { in             Try { - fis.close (); to}Catch(IOException e) { +                 //TODO auto-generated Catch block - e.printstacktrace (); the             } *         } $     }Panax Notoginseng  -     //Common run-time exceptions the     //4. Null pointer exception: Nullpointerexcetion + @Test A      Public voidTest5 () { the         //Person p = new Person (); +         //p = null; -         //System.out.println (p.tostring ()); $  $         Try { -String str =NewString ("AA"); -str =NULL; the System.out.println (Str.length ()); -}Catch(Exception e) {Wuyi             //e.printstacktrace (); theSystem.out.println ("An exception occurred with a null pointer"); -         } Wu     } -  About     //3. Type Conversion exception: ClassCastException $ @Test -      Public voidtest4 () { -         Try { -Object obj =NewDate (); AString str =(String) obj; +}Catch(classcastexception e) { theSYSTEM.OUT.PRINTLN ("Exception occurred with type conversion"); -             //System.out.println (10/0); $}Catch(Exception e) { the e.printstacktrace (); the}finally { theSystem.out.println ("Hello! beauty! "); the         } -         //String str1 = (string) new Date (); in     } the  the     //2. Arithmetic exception: ArithmeticException About @Test the      Public voidtest3 () { the         Try { the             inti = 10; +System.out.println (i/0); -}Catch(Exception e) { the             //e.printstacktrace ();Bayi System.out.println (E.getmessage ()); the         } the     } -  -     //1. Array subscript out-of-bounds exception: ArrayIndexOutOfBoundsException the @Test the      Public voidtest2 () { the         Try { the             int[] i =New int[10]; -             //System.out.println (i[10]); theSystem.out.println (i[-10]); the}Catch(Exception e) { theSystem.out.println ("There's an exception!" ");94         } the     } the  the @Test98      Public voidtest1 () { AboutScanner s =NewScanner (system.in); -         Try {101             inti =s.nextint ();102 System.out.println (i);103}Catch(inputmismatchexception e) {104System.out.println ("There is a type mismatch exception!") "); the         }106     }107}

Java Learning--The handling of abnormal exception

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.