I summarized the types and basics of Java exceptions
Java exception (error)
Exception handling important for writing Java
What kind of exceptions exist in Java in order to handle the exception properly?
What to do
I think it is important to suppress that, so I investigated and summarized Java exceptions again.
This article doesn't write any code, only the basics about Java exceptions.
Error type
There are three types of errors.
Of these, only run-time errors need to be handled as exceptions.
Syntax error
- Compiling error
Example) Misspelling etc.
Runtime error
- The grammar is correct and it compiles
- Compiling passes, but an abnormal situation occurs during execution, and program execution is forcibly terminated.
=> The error at this time is called a run-time error (because it is an error that occurs during execution)
Example) Trying to process null, accessing an index that does not have an array, etc.
Logic error
- Can be compiled and executed, but an error such as returning a value different from the expected value
- The logic of the code I wrote was wrong
What are the exceptions in the first place?
- Run-time errors are caused by "unexpected events that occur during program execution"
=> This unexpected event is an "exception"
Then what is exception handling?
- Exception handling (error handling) is to handle an exception that is an "unexpected event that occurred during program execution".
Exceptions to check and exceptions to not
- Exceptions that should be checked are called "checked exceptions", and exceptions that do not need to be checked are called "unchecked exceptions".
- The exception that should describe exception handling (error handling) is this checked exception.
Exception type
The exceptions are roughly divided into the following three
- Error type: Exception that is unlikely to recover
- Exception type: Exception for which recoverable measures should be taken
- RutimeException series: Exceptions that should never be dealt with
Of these, the only exceptions to be handled (checked exceptions) are Exception-type exceptions.
Check exception (check exception)
- Among run-time errors, exceptions that are likely to be recovered and need to be dealt with in consideration of the occurrence of the exception.
- In other words, the programmer should handle exceptions (error handling) only for runtime errors && Exception type exceptions.
- This checked exception (checked exception) will result in a compile error unless exception handling (an act that is often said to catch) is performed in the code in advance.
Unchecked exception (unchecked exception)
- Exceptions other than the above checked exceptions
- Exceptions that the programmer does not need to handle
The site that I used as a reference
http://qiita.com/yuya_presto/items/3b651d6b0cf38f77e933
http://qiita.com/kata/items/bd129ba6113a61126389
http://qiita.com/yuba/items/d41290eca726559cd743
References
Kiyotaka Nakayama, Daigo Kunimoto (2011/10/7) "Introduction to Java for a refreshing understanding" Impress Japan
- The same article is posted on the blog
http://www.sekky0905.com/