Atitit. Usage of exceptions summary best practices java. net php Vo8f, atititvo8f

Source: Internet
Author: User
Tags try catch what php

Atitit. Usage of exceptions summary best practices java. net php Vo8f, atititvo8f

Atitit. java exception usage summary best practices Vo8f

 

1. Why is exception 1

2. throw an exception to the catch clause and passing a parameter through function call are basically the same. 2

3. primary motivation of s e h 2

4. RuntimeException and checked Exception 2

5. Do you want to use checked Exception 3?

6. For Exception business process control, refer to) 3

7. disadvantages of checked Exception 4

8. The Base class and extended class throws special exceptions and different solutions 4

9. checked Exception conversion re 4

10. New exception D is thrown, which can be inherited from existing exceptions. 4

11. List the five most commonly used RuntimeException: 5

12. The following are the Exception classes listed in the jdk api: All except RuntimeException are checked Exception 5

13. Refer to 6

 

 

1. Why is the usage abnormal?

 

For C Programs, you can use Error Code. Why should we introduce exceptions? The exception cannot be ignored. If a function sets a state variable or returns an error code to indicate an abnormal state, there is no way to ensure that the function caller will check the variable or test the error code. The result program continues running from the exception state it encountered. If the exception is not captured, the program will immediately terminate the execution.

 

 

In the C program, we can use int setjmp (jmp_buf env); and void longjmp (jmp_buf env, int value); these two functions to complete the functions that are familiar with exception handling, however, MSDN introduces that longjmp cannot be used to call destructor for partial objects when stack is adjusted in C ++, but for C ++ programs, destructor are important (I usually put the Delete object in the destructor ). <Br> therefore, we need a method: ① To notify the exception status, but not to ignore this notification. ② when Searching the stack to locate the Exception Code, ③ make sure that the destructor of a local object is called. The exception handling of C ++ is just to solve these problems. </P> <p> in some cases, only exceptions can be used to solve the problem. For example, an error type cannot be captured or determined in the current context, we need to throw an exception to a larger context. In addition, the use of exception handling can separate the error handling program from the "common" code to make the code more concise and flexible. In addition, the program is essential for robustness, and Exception Handling often plays an important role in it. </P>

 

 

Because when a function returns, a local object is always released, no matter how the function exits. (Only one exception is when you call longjmp. This disadvantage of Longjmp is that C ++ is the first to support exception handling)

 

Although C ++ introduced the exception specification for the first time

Although C ++ introduced the Exception specification for the first time, Java is the only mainstream language that enforces the "Checked Exception" Specification.

 

 

Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://blog.csdn.net/attilax

 

2. throw an exception to the catch clause and passing a parameter through function call are basically the same.

There are some similarities, but they are also very different. </P> <p> let's start with the similarities. The way to pass function parameters and exceptions can be to pass values, pass references, or pass pointers, which is the same. However, when you pass parameters and exceptions, the operation process to be completed by the system is completely different. The reason for this difference is: when you call a function, the control of the program will eventually return to the function call, but when you throw an exception, control will never return to the place where an exception is thrown. </P>

 

3. primary motivation of S E H

Microsoft introduced s e h in Wi n d o w S to facilitate the development of the operating system. Operating system developers use s e h to make the system stronger. We can also use s e h to make our own programs stronger. <Br> the burden caused by the use of s e h is mainly borne by the Compilation Program, rather than the operating system. <Br> when an exception block occurs, the compiler generates special code. The compile program must generate some tables (t a B l e) to support processing the data structure of S E H. <Br> the compiler must also provide callback (c a l B a c k) functions. The operating system can call these functions to ensure that exception blocks are processed. <Br> the Compilation Program is also responsible for preparing the stack structure and other internal information for use and reference by the operating system. <Br> it is not easy to add the s e h support in the Compilation Program. It is not surprising that different compiler manufacturers implement s e h in different ways. Fortunately, we don't have to consider the implementation details of the Compilation Program, but only use the s e h function of the Compilation Program. (In fact, most compiler vendors use the syntax recommended by Microsoft)

 

 

4. RuntimeException and checked Exception

Java Exception is divided into two types: RuntimeException and its subclass, and checked Exception. Java requires the function to write the checked Exception that has not been caught.

. Net only has RuntimeException

Except for Error and RuntimeException, you need to care about the remaining exceptions. These Exception classes are collectively referred to as Checked exceptions. As for Error and RuntimeException, they are collectively referred to as Unchecked exceptions.

 

5. Do you want to use checked Exception?

To use... the advantage is the process clr, and ide can automatically generate structure code...

 

We recommend that you use the checked exception in case of recoverable errors and unchecked exceptions in case of unrecoverable exceptions.

 

 

6. For Exception business process control, refer)

When UseCase is used to describe a scenario, there is a main event stream and n abnormal streams. The exception stream may occur in the main event stream process, while the try statement implements the main event stream, while the catch statement implements the exception stream, here, Exception does not indicate an Exception or error in the program. Exception is only an object-oriented business logic control method. If I don't understand this, I don't think I really understand how to use Java for proper programming.
The program I write will customize a large number of Exception classes. All these Exception classes do not mean that the program has encountered exceptions or errors, but only represent the occurrence of non-main event streams, used to control the process of those branch processes. For example, if you add a user to the permission system, you should define an exception class, UserExistedException. If this exception is thrown, you cannot insert the table. This only indicates that you encounter a branch process, wait for the catch to process the branch process. Traditional programmers will write an if else for processing, while a qualified OOP programmer should consciously use the try catch method to differentiate the processing of the main event stream and n branch flows. Through try catch, instead of if else, different event streams are isolated from the code for separate code writing.

 

 

7. disadvantages of checked Exception

Unchecked exception supported:
The Checked exception spreading up the call stack destroys the top-level methods, because these methods must declare that they throw all the methods they call and throw the exception.

 

The Check exception is thrown as part of the method interface, which makes it difficult to add or remove methods in earlier versions.

8. The Base class and extended class throws special exceptions and different solutions

You can add the declaration of throwing ExceptionB to the foo method of the Base class. However, this destroys the open-close principle. In addition, sometimes we cannot modify the parent class, for example, when reloading a class in Jdk.

 

Another possible practice is to catch ExceptionB In the foo method of Extend, construct a regiontiona, and throw it. This is a feasible approach, but it is only a matter of expediency.

 

9. checked Exception conversion re

To avoid writing throws in function declaration, the following code is often seen in Java projects to 'swapp' Exception:

 

 

10. Throwing a new exception D can be inherited from an existing exception,

[Iii] in The "The Design and Evolution of C ++", Bjarne Stroustrup also mentioned that it is also based on consideration of this issue, c ++ uses "Run time Checking" instead of "Static Checking" (checked exceptions ". In addition, Stroustrup suggests that if a new exception D is thrown, it can be inherited from the existing exception so that the existing code is not affected, and the new Code can also be processed. (This is the conclusion of Stroustrup in June 1990 !)

 

 

 

11. List the five most common runtimeexceptions:

This is the most common question in the JAVA certification examination. In fact, runtime exception is the most common and frequently encountered, that is, five or six types, as shown below:

ArithmeticException

Int a = 0;
Int B = 3/;

ClassCastException:

Object x = new Integer (0 );
System. out. println (String) x );

IndexOutOfBoundsException
ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException

Int [] numbers = {1, 2, 3 };
Int sum = numbers [3];

IllegalArgumentException
NumberFormatException

Int a = Interger. parseInt ("test ");

NullPointerExceptionextends

 

 

· Apart from RuntimeException, other exceptions inherited from java. lang. Exception are collectively referred to as Checked Exception. What are their types?

12. The exception classes listed in JDK APIs are as follows:
All except RuntimeException are checked exceptions.

Java. lang. Object
Java. lang. Throwable
Java. lang. Exception
All Implemented interfaces:
Serializable
Directly known subclass:
Failed, ActivationException, failed, ApplicationException, AWTException, BackingStoreException, failed, failed, BadLocationException, failed, DataFormatException, DatatypeConfigurationException, failed, ExecutionException, ExpandVetoException, FontFormatException, generalSecurityException, GSSException, failed, failed, InstantiationException, failed, IntrospectionException, failed, InvocationTargetException, IOException, JAXBException, JMException, KeySelectorException, failed, failed, lost alexception, failed, failed, failed, failed, NamingException, failed, NotOwnerException, ParseException, ParserConfigurationException, PrinterException, PrintException, failed, reconfiguralexception, RuntimeException, SAXException, ScriptException, ServerNotActiveException, SOAPException, SQLException, TimeoutException, begin, TransformerException, TransformException, failed, URIReferenceException, failed, UserException, XAException, XMLParseException, XMLSignatureException, XMLStreamException, XPathException

 

 

13. Reference

Why not introduce java-like checked exceptions-products and technologies-SCID .htm in c #

C00000000.htm

 

 

[Reprinted] JAVA checked and unchecked _4527_sina blog .htm


What are the development prospects of php? Will it be replaced by Java or net? What are the advantages of php over Java and net?

The development prospects are acceptable and will not be replaced. Compared with JAVA and. net, it has the following advantages: easy to learn, easy to deploy, cross-platform, and suitable for quick development of small websites.

To develop PHP, you have to go to senior engineers.

Ask what PHP and java NET do, but it is better in employment.

From the perspective of learning, Java is the most difficult to learn ,. net second, PHP is best to learn .... from the perspective of web development, PHP is the best site to use. ASP.net is the second, and Java is the least useful ..... no matter which one has learned well, he can find more than 5000 jobs...
PHP has A golden combination of PAML, P: php a: Apache server M: MySQL database L: Linux operating system, because these four are free of charge, therefore, PHP development has a relatively small investment in some sense, and small companies are generally frustrated in using it...
ASP.net is a set of Microsoft systems that need to learn the C ++ ASP language and so on, because I did not do this, so it is not clear ..
Next, let's talk about Java as an object-oriented language. First, you should learn Java programming. After learning it, you want to develop Java web. You should also learn a lot about it. 1: jsp servlet must be proficient in 2: at least need to know more than one type of database MySQL or oracle or other, 3: html javascript you have to, otherwise how to write JSP page 4: tomcat WebLogic, where else will your program run... next, you need to learn more about frameworks, such as the SSH framework. There are a lot of things to learn ...... at least you need to know the Java design pattern... in fact, because of the Java design pattern, simple things are often complicated .... haha... it is actually the most important thing to do with business processes... of course, Java can be used for mobile game development, but I don't know much about this .... hope it will help you ....
In general, all three of them are relatively studious .... getting started is relatively simple... but it is the most difficult to master Java... PHP should be the easiest. asp.net is the second .. it depends on how the landlord chooses... as for employment, the three are currently popular... I don't know what will happen in the future ....
Hope to help the landlord. I wish you a smooth learning ....

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.