Why RunTimeExceptions do not need to be caught at compile time

Checked exceptions must be caught or declared in order to be thrown at compile time, but no exception execution time is required ... why do we give importance only to a checked exception ...

+3
source share
8 answers

In Java, the original inventors of the language wanted to distinguish between the common types of exceptions that might occur in a program. They came up with these three types:

  • Checked exceptions are used for errors that may occur at runtime and are expected (sort of), for example IOException. For example, when working with file or network I / O, an error may occur (at any time on the disk, loss of connection, etc.). The programmer must know that any operation called up can fail at any time, and therefore the language provides such an understanding, forcing the programmer to do something with this exception.

  • , NullPointerException, IllegalArgumentException .. . , , , . , ( , , ? - ", " )

  • , , OutOfMemoryError, AssertionError .. . , , , , .

, ( , ) Error s. , Spring, , , ( ) . , # , . , .

+10

http://en.wikipedia.org/wiki/Exception_handling

, , , . , : RuntimeExceptions , [19] , JVM . , , , ,

, ,

+2

: , , , (: " " , ). Runtime ( ), .

, . . , , ( !).

, 1 / 0 ArithmeticException, 1.0 / 0 Infinity? - ... java.text.Format String, - (IllegalArgumentException), , , - (, null), try-catch , Format. , - , . . . JDK , .

. , ; , - . , , try-catch -block , : " " ( ). ( , ). ( try-catch). -, , .

, , . catch(Exception ex) .

+2

"RunTime".

, .

/ , , .

. int, . . ,

+1

RuntimeExceptions:

  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • ClassCastException
  • EmptyStackException
  • IllegalArgumentException
  • IllegalMonitorStateException
  • NullPointerException
  • UnsupportedOperationException

: , , , - . JVM, , , .

+1

, JVM .

unchecked exceptions are occurred due to poor logic of our program.so program will be terminated abnormally.but JVM will not be halted.so JVM creates Excpetion object.

, , - , JVM . - JVM ​​.so, . .

, compiletime, throw.

, - JVM , throw, , object.so, , .

+1
    When you can handle the recovery of the state of Object , go for Checked Exception.
    When you cannot handle the recovery go for UnCheckedException.
    Mostly API developers go for Runtime Exception , 
   they do not want to enforce handling exception by the user, 
   since they themselves do not know how to handle it
0
source

Runtime exceptions can occur anywhere in the program, but in a typical they can be very numerous. The need to add runtime exceptions in every method declaration will reduce program clarity. Thus, the compiler does not require you to catch or throw runtime exceptions (although you can).

0
source

Source: https://habr.com/ru/post/1789215/


All Articles