JAVA API Exception Handling __java

Source: Internet
Author: User
Tags finally block getmessage stub throw exception throwable
 example one: exception handling mechanism in/** * java * @author Administrator * * */public class ExceptionDemo1 {public static VO

		ID Main (string[] args) {System.out.println ("program started");
			try{String str = null;
			 * * The JVM found STR null when executing the Str.length () method.
			 * Then create a nullpointerexception instance, encapsulate the description of the error, and then throw out the exception. * The JVM then looks at whether the following code is surrounded by a try statement, if not, that the current method does not capture the exception's energy force, then throws the exception out of the method, as it is thrown outside the * Main method.
			 The JVM terminates the current program. * If a try is surrounded, the null pointer exception instance is passed to NullPointerException e in Catch *, and the * content of the catch is executed to resolve the exception.
			 When resolved, the program continues to perform normally.
			
		* * SYSTEM.OUT.PRINTLN ("Length:" +str.length ());
		}catch (NullPointerException e) {System.out.println ("A null pointer has appeared!");
	} System.out.println ("End of Program"); }
}
/**
 *  multiple catch
 * @author Administrator
 * * */public
class ExceptionDemo2 {public
	static void Main (string[] args) {
		System.out.println ("program started");
		try{
			String str = "AA";
			System.out.println ("Length:" +str.length ());
			System.out.println (Str.charat (0));
			SYSTEM.OUT.PRINTLN (Integer.parseint (str));	
			
		catch (NullPointerException e) {
			System.out.println ("A null pointer appears");
			
		} catch (Stringindexoutofboundsexception e) {
			System.out.println ("Subscript crossed out");
		 * * To have a good habit of capturing the maximum * exception in the last catch
		 . This prevents a program from being interrupted by a * exception that is not caught in a try * when 
		 defining multiple catch, be aware that if a parent-child relationship's exception
		 * appears at the same time, the parent class exception is defined under the subclass exception.
		 * 
		 *
		/}catch (Exception e) {
			System.out.println ("It's a mistake!");
		}
		
		System.out.println ("End of Program");
	}


/**
 * Finally block
 * Finally can be followed directly after the TRY statement block
 * or after the last catch
 * The code in the finally block guarantees that our program will be executed whether it is wrong
 or not. * * 
 @author Administrator
 *
/public class ExceptionDemo3 {public
	static void main (string[ ] args {
		System.out.println ("program started");
		
		try {
			
			String str = "";
			System.out.println (Str.length ());
			SYSTEM.OUT.PRINTLN ("!!!!");
			
			
		} catch (Exception e) {
			System.out.println ("Error!");
		} finally{
			System.out.println ("Finally code must execute!");
		
		System.out.println ("End of Program");
	}


/**
 * finally face question
 * 1:final finally finalize what is the difference?
 *   Finalize is a method defined in object. This method is executed by the JVM
 *   This method is invoked when an object is about to be reclaimed by GC.
 * 
 * 2: The following example
 * * 
 @author Administrator
 */public
class ExceptionDemo4 {
	public static void Main (string[] args) {
		System.out.println (
				test ("0") + "," +test (NULL) + "," +test (")
		);
	} public
	
	static int test (String str) {
		try {return
			Str.charat (0)-' 0 ';
		} catch (NullPointerException e) {return
			1;
		\ catch (Exception e) {return
			2;
		} finally{return
			3;
		}
}

/**
 * Throw is an active throw exception
 * Usually throws an exception in two cases:
 * 1: satisfies the syntax requirement, but does not satisfy the business logic
 * 2: The exception code should not be responsible for handling the exception (liability issue) 
 * @author Administrator
 *
 *
/public class ExceptionDemo5 {public
	static void Main (string[] args) {
		person who = new person ();
		 * * When we call a method that declares a throws, the compiler asks
		 * We must handle the exception that may be thrown by the method in the calling method here
		 :
		 * 1: try-catch capture and process yourself
		 * 2: Continue to throw the exception to the current method continue to throw out
		 * 
		 * never again on the main method declaration throws!
		 */
		try {person.setage ()
			;
		} catch (Illegalageexception e) {
			e.printstacktrace ();
		}
		
		System.out.println ("Age is:" +person.getage ());
		
		
	}


Example two:

 Some common methods are defined in/** * exception *
 @author Administrator */Public
class ExceptionAPIDemo1 {
	public static void Main (string[] args) {
		System.out.println ("program started");
		try {
			String str = "AA";
			SYSTEM.OUT.PRINTLN (Integer.parseint (str));
		catch (Exception e) {
			//Output error stack (procedure execution, until the location of error)
			e.printstacktrace ();
			String GetMessage () Gets the wrong information
			System.out.println (E.getmessage ());
		}
		System.out.println ("End of Program");
	}


/**
 * Exception API 2
 * @author Administrator */Public
class ExceptionAPIDemo2 {
	public static void Main (string[] args) {
		try {
			dosome ("");
		} catch (Exception e) {
//			e.printstacktrace ();
			System.out.println (E.getcause ());
		}
	
	
	public static void Dosome (String str) throws exception{
		try {
			System.out.println (Str.charat (0));
		catch (NullPointerException e) {
			throw new Exception (e);
		} catch (Stringindexoutofboundsexception e) {
			throw new Exception (e);
		}
	}
Example three:
/**
 * Custom Exception
 * This exception represents an age-illegal error
 * Usually we can
 use a custom exception to describe some of the business logic errors that are occurring in the current program. * * 
 @author Administrator
 */public
class Illegalageexception extends exception{

	private Static final Long serialversionuid = 1L;

	Public illegalageexception () {
		super ();
		TODO auto-generated constructor stub
	} public

	illegalageexception (String message, throwable cause) {
		Super (message, cause);
		TODO auto-generated constructor stub
	} public

	illegalageexception (String message) {
		super (message);
		//TODO auto-generated constructor stub
	}

	Public illegalageexception (Throwable cause) {
		super (cause);
		TODO auto-generated constructor stub
	}	



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.