A summary of the mystery of anomalies

Source: Internet
Author: User
1. Concept of accidental termination and normal termination

End of normal: executed in the order of the statements.

Unexpected end: For example, Break,continue, and so on, execute the following statement sequentially.

public class puzzlerdemo36{public
	static void Main (String args[]) {
		System.out.println (decision ()); 
		
	}
	public static Boolean decision () {
		try{return
			true;
		}
		finally{return
			false;}}


2.try-finally Rules

Characteristics:

(1) When the TRY statement block and finally statement block end unexpectedly, the reason that the try block throws an unexpected end is discarded.

Advice:

(1) Do not accidentally end in a finally statement block, that is, you cannot use return, break, continue to jump out of the finally statement block in finally, and do not allow the checked exception to propagate to finally.

(2) Note: Only the try statement will enter the finally statement block, so if the try statement does not end, it will never enter finally.

(3) System.exit (0) is a statement that allows a try statement block to be closed.

When this statement is invoked, (1) The JVM stops the current thread and the death path. (2) Close Hook (3) Close Terminator

(4) System.halt (); Stop the VM without closing the hook.

public class puzzledemo39{public
	static void Main (String args[]) {
			System.out.println ("Hello");
			Runtime.getruntime (). Addshutdownhook (New Thread () {public
				void run () {
					System.out.println ("GoodBye"); 
				}
			) ;
			System.exit (0);
	}
}


3.try-catch Rules Features:

(1) If a catch clause catches a checked exception, you must be able to throw the checked exception in the Try statement, or you will compile an error.

(2) If exception or Throwable are captured in a catch, the try statement is arbitrary. 4. Method throws Exception stipulation

The collection of checked exceptions that a method can throw is the intersection of all the checked exception sets that he declares to throw.

In simple terms, the A method is the intersection of multiple inherited throws,

Example

Interface A {
public void Fun () throws clonenotsupportedexception,interruptedexception;
}
Interface b{
public void Fun () throws interruptedexception;
}
Class c{
The intersection of the public void Fun () throws interruptedexception{//inheritance method throws
}
}
The collection of exceptions that a method can throw is the intersection of checked exceptions that the type declaration that he applies to throw.
import java.io.*;
public class PuzzleDemo37 implements type3{public
	void F () {
		System.out.println ("Hello"); 
	}
	public static void Main (String args[]) {
		Type3 t3 = new PuzzleDemo37 ();
		T3.F ();
	}
Class arcane1{public
	static void Main (String args[]) {
		try{
			System.out.println ("Hello World"); 
		}
		catch (Exception e) {
			System.out.println ("Error1");
}} Class arcane2{public
	static void Main (String args[]) {
		try{
		
		}
		catch (Exception e) {
			System.out.println ("Error2");
}} interface type1{
	void F () throws clonenotsupportedexception;
}
interface type2{
	void F () throws interruptedexception;
}
Interface Type3 extends type1,type2{
}


rules for 5.final variables

An empty final variable can be assigned only where he does not have a value assigned to it.

That is, if


You cannot compile because the compiler takes a conservative attitude, even if you are sure it is safe.

Solution: Take advantage of accessibility functions:


Conclusion:

If you must refactor a program to eliminate the error that is thrown by a definite assignment, you must consider adding a new method.

public class puzzledemo38{public
	static final long guest_user_id = -1l;
	Private static final long user_id;
	static{
		try{
			user_id = Getuseridfromenvironment ();
		}
		catch (Idunavailableexception e) {
			user_id = guest_user_id;
			System.out.println ("Logging in as Guest"); 
		}
	private static long Getuseridfromenvironment () throws idunavailableexception{
		throw new Idunavailableexception () ;
	}
	public static void Main (String args[]) {
		System.out.println ("user_id:" +user_id); 
	}
Class Idunavailableexception extends exception{
	idunavailableexception () {}
}


6. The provisions of the class

(1) Instance variable initialization takes precedence over the constructor's program body.

(2) The constructor must declare all the checked exceptions that the instance variable may throw.


7. Flow Regulation

For exceptions that may be thrown in finally, they need to be processed and not propagated arbitrarily.

For the Close () method in Finally, it must be captured.

Import java.io.*;
public class puzzledemo41{public
	static void Main (String args[]) throws exception{
		copy ("Src.txt", "des.txt");
	}
	public static void Copy (String src,string des) throws ioexception{
		inputstream in = null;
		OutputStream out = null;
		try{in
			= new FileInputStream (SRC);
			out = new FileOutputStream (DES);
			Byte[]b = new byte[1024];
			int n;
			while ((N=in.read (b)) >=0) {
				out.write (b,0,n);
			}
		} finally{
			try{
				if (in!=null)
					in.close ();
			}
			catch (IOException e) {
				System.out.println ("in"); 
			}
			try{
				if (out!=null)
					out.close ();
			}
			catch (IOException e) {
				System.out.println ("Out");}}}



8.& and &&, a series of logical and conditional operators

A&b: Specify whether a is true or false,b to be calculated and to produce results.

A&&b: If A is false, B can not calculate and return false directly.

A|b: Specify whether a is true or false,b to be calculated and to produce results.

a| | B: If A is true, B can not calculate and return true directly.


9.StackOverFlowError Introduction

is usually caused by infinite recursion.

There is a call stack in the JVM, and he presets a depth, and when that depth is exceeded, a stackoverflowerror is thrown.

So we want to avoid infinite recursive invocation.

Import java.io.*;
public class puzzledemo40{public
	static void Main (String args[]) throws exception{
		try{PuzzleDemo40
			= New PuzzleDemo40 ();
			System.out.println ("1"); 
		}
		catch (Exception e) {
			System.out.println ("Exception occurrence"); 
		}
	Private PuzzleDemo40 PD1 = new PuzzleDemo40 ();
	Public PuzzleDemo40 () throws exception{
		throw new Exception ("intentionally");
	}

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.