3.16 Learning experience

Today, I mainly learned the singleton pattern in Java and common exception error handling.

In actual development, most of the service classes will be designed as singletons.The so-called singleton mode is that the class has only one object, and the external use of the object of this class is implemented by calling a class method.

/**
   * Full Chinese singleton mode
 * @author lzq31
 *
 */
public class Service2 {
	
	private static Service2 service = new Service2();
	
	private Service2() {
		
	}
	
	public static Service2 getInstance() {
		return service;
	}
}
/**
   * Hungry Chinese singleton mode
 * @author lzq31
 *
 */
public class Service {
	
	private static Service service;
	
	private Service() {
		
	}
	
	public static Service getInstance() {
		if (null == service) {
			service = new Service();
		}
		return service;
	}
}


Common ways to handle exceptions

try catch :

To judge whether an object is empty, usenull != str

In program method design, especially method implementation, the passed-in parameters are unknowable, so the passed-in parameters must be verified before they can be used, for example, to determine whether they are null.

public static void fun1(String str, String subStr) {
    if (null != str && null != subStr) {
        if (str.indexOf(subStr) >= 0) {
            System.out.println("Substring exists");
        } else {
                         System.out.println("There is no substring");
        }
    } else {
                 System.out.println("Illegal string");
    }

}

In the try catch structure, catch can be omitted, or multiple exceptions can be caught, but when multiple exceptions occur, the parent exception can only appear at the bottom.

In actual development, the simplest way is to use an Exception to handle directly


finally :

It will be executed regardless of whether there is an exception or not. In particular, if you do not write catch, then finally will be executed, but the statement after the interrupt will not be executed.

public class Demo1 {
	public static void main(String[] args) {
		String str = "abc";
		str = null;
		fun1("abc", null);
	}

	public static int div(int m, int n) {
		int k = m / n;
		return k;
	}

	public static void fun1(String str, String subStr) {
		try {
			System.out.println("11111111");
			int i = 10 / 0;
			if (str.indexOf(subStr) >= 0) {
				 System.out.println("Substring exists");
			} else {
				 System.out.println("There is no substring");
			}
			System.out.println("end");
		} catch (Exception ex) {
			 System.out.println("An error occurred in the program");
			ex.printStackTrace();
			System.out.println("#"+ex.getMessage());
			
		} finally {
			 // A block of code that must be executed
			System.out.println("finally");
		}
		
		System.out.println("fun1 end");

	}
}


throws:

inMethod definitionThe following explicit declaration method is abnormal, the program that calls this method must explicitly handle the exception, and the latter can also be thrown again.

public class Demo2 {
	public static void main(String[] args) {
//		fun1();
//		fun2();
		try {
			fun3();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
	}
	
	public static void fun1() {
		System.out.println("begin");
		int i = 0;
		try {
			i = 10 / 0;
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		
		System.out.println(i);
		System.out.println("end");
	}
	
	 // Use throws to throw an exception to notify the program that called the method that you have to deal with it.
	public static void fun2() throws Exception{
		System.out.println("begin");
		int i = 0;
		i = 10 / 0;
		System.out.println(i);
		System.out.println("end");
	}
	
	public static void fun3() throws Exception {
		System.out.println("begin");
		int i = 0;
		try {
			i = 10 / 0;
		} catch (Exception ex){
			 // No processing
			 throw new Exception("System Error");
		}
		System.out.println(i);
		System.out.println("end");
	}
	
}

throw:

Create a custom exception object. When a general exception occurs, the virtual machine dynamically creates a "system exception" and throws it, which is a concept with custom throwing.

public class Demo3 {
	public static void main(String[] args) throws Exception {
		fun1(17);
		System.out.println("end");
		
	}
	
	public static void fun1(int age) throws Exception {
		if (age < 18) {
			 throw new Exception("Your age is illegal");
		} else {
			 System.out.println("You have entered the VIP room");
		}
	}
}

    

Intelligent Recommendation

2021 3.16

Calculator operator And to: 197.56 The difference is: 106.44 Accumulation: 6925.12 Business is: 0.29973686 Where N1, F indicates that the number of assignments is a FLOAT type, which cannot go, otherw...

Practice-3.16

Array example: number...

"Deep Learning of Hands-on Learning" study notes ------3.16. Actual Kaggle competition: house price forecast (1)

"Deep Learning of Hands-on Learning" study notes ------3.16. Actual Kaggle competition: house price forecast Pretreatment Some features are continuous values, such as: MSSubClass; some featu...

C ++ learning experience and experience

C ++ learning experience and experience The functionality of the computer is started from the calculation. Therefore, first of all, the data operation is to be performed. Since it is necessary to perf...

Python learning experience and experience

This is my first blog issued a text, and I will record the experience of my beginners Python. Python is easy to get started with other computer languages, and the development prospects are good. The s...

More Recommendation

Learning experience

Learning experience 1. Research related top meeting 1. World recognized three major conferences on computer vision CVPR, ICCV, ECCV Next to the above meetings are ICIP (International Conference on Ima...

Learning that experience?

Yes, and the same night, I was at the computer, in fact, still a little sore right hemisphere, more and more overwhelmed, not too many things you learn is very slow, it seems there will not be how wil...

Take the Stone Series-Four Take the Stone (6) (Nim Game)

Take stones (6) Time limit: 1000 ms | Memory limit: 65535 KB Difficulty: 3 Description Recently, TopCoder’s PIAOYI and HRDV are very boring, so I thought of a game. The game is like this: there ...

eclipse encountered errors summary

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassP...

Dictionary Learning (learning dictionary, sparse representation and others)

The first part of the study and a summary of dictionaries sparse representation Dictionary learning (Dictionary Learning) and sparse representation (Sparse Representation) in the official title of the...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top