Dark Horse Programmer ———— Java Base exception handling

Source: Internet
Author: User
Tags getmessage

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

  

1. Architecture
Java.lang.Object
|----java.lang.Throwable
|-------java.lang.Error: errors, Java programs do nothing about this, do not explicitly handle
|-------java.lang.Exception: Exception. Need to be processed
|------runtimeexception: Run-time exception
|-----arrayindexoutofboundsexception/nullpointerexception/arithmeticexception/classcastexception
|------non-runtimeexception: compile-time exception

2. Because Java programs are divided into Javac.exe and java.exe two processes, exceptions can occur in each process. It is divided into compile-time exception, run-time exception
2.1 is common for run-time exceptions and can be handled without explicit processing.
2.2 For compile-time exceptions, you must explicitly handle
Compile-time exception, not to say that there is an exception to deal with, but there is a hidden danger, must be in the compilation, prompt program, in case of an exception, how to deal with!

2. How do I handle exceptions?
"Scratch-and-throw model" in Java
* 1. " 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{    //Possible exception code     }catch (Exception1 E1) {     //Handling Way 1     }catch (Exception2 E2) {     //processed by 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.
Treatment Method Two:
At the declaration of the method, use the throws + exception type explicitly

public void Method1 ()  throws Exception1 E1,exception2 e2{//possible exception (especially compile-time exception, must be handled)     } public        void Method2 () Throws Exception1 E1,exception2 e2{method1 ();    }     public void Method3 () {try{method2 ();} catch (Exception1 E1) {System.out.println (E1.getmessage ());    } catch (Exception2 E2) {System.out.println (E2.getmessage ());    }     }   public static void Main (string[] args) {object 1.method3 ();//The above Exception1 and Exception2 exceptions will not appear again!   }

  

3. How do I manually throw an exception?
Inside the method, you can use the Throw + Exception class object to manually throw an exception!

Compares the size of a radius of two circles. public int compareTo (Object obj) throws exception{if (this = = obj) {return 0;} else if (obj instanceof circle) {Circle c = (circle) obj;if (This.radius > C.radius) {return 1;} else if (This.radius = = C.radius) {return 0;} else{return-1;}} else{//return-2;//manually throws an exception//throw new Exception ("The incoming type is wrong!") ");//throw new String (" The incoming type is wrong! "); throw new MyException (" The incoming type is wrong! ") ");}}

  

4. How do I customize an exception class?
> Throws an exception manually, in addition to throwing an object out of the exception class that is out of the box, you can also throw an object with a custom exception class!
> How do I customize an exception class?

public class MyException extends exception{static final long serialversionuid = -70348975766939l;public myexception () {} Public myexception (String msg) {super (MSG);}}

  

Dark Horse Programmer ———— Java Base 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.