-
Notifications
You must be signed in to change notification settings - Fork 0
Task_14
##Задание
####Теория Напиши чем отличаются исключения Exception от RuntimeException; Напиши чем отличаются исключения RuntimeException от Error; Есть ли у исключений конструктор с параметром String и как он используеться? Напиши что такое throw и для чего используется Напиши что такое throws и для чего используется Напиши когда необходимо создавать собственные исключения;
####Практика Создай свой собственный класс исключений (можешь назвать его ComputerAccessException). Модифицируй свою программу таким образом чтобы исключение возникало в том случае когда пользователь будет использовать калькулятор при выключеном компьютере. Например: ComputerAccessException:”You can't use this program because computer is off”
Вот ссылка для лучшего понимания исключений: http://www.quizful.net/post/java-exceptions
##Ответ (ru)
####Теория
####Практика
##Ответ (en)
####Теория
- Difference between checked exceptions and Runtime Exceptions.
-
Checked exceptions consist of the java.lang.Exception class and all of its subclasses; except for java.lang.RuntimeException and its subclasses
-
Checked exceptions are exceptions that the Java compiler forces you to handle by either catching the exception, or re-throwing it. The failure to handle a checked exception results in a compiler error.
-
Runtime exceptions serve the same purpose as checked exceptions; to communicate exceptional conditions (unexpected failures, etc) to the user. They can be thrown and caught just like checked exceptions. However, handling runtime exceptions is not enforced by the compiler.
- Difference between runtime exception and Error:
-
Both Error and RuntimeException are unchecked exceptions, meaning that it indicate a flaw with the program, and usually should not be caught. for instance (NullPointerException, IndexOutOfBoundsException)
-
The main difference between the two is that RuntimeException indicate there is a error with the program, and an Error is something that is fatal but out of the program's control. (OutOfMemorryError, ThreadDeath etc.) therefore subclassing an error is bad practice because an error is usually not something that could be fixed by your program at runtime.
- Does an exception have a constructor with String parameter:
- You need to define the one taking a String in your class. Typically you use super(message) in your constructor to invoke your parent constructor. For example, like this:
public class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
- Throws
- Throws clause in used to declare an exception in method declaration (signature).
- Throw
- Thow keyword is used to throw an exception explicitly inside method body to invoke an exception.
- Custom exceptions
- You should write your own exception classes if you need an exception type that isn't represented by those in the Java platform
####Практика