Question:
what is difference between error and exception?
vj
2012-10-05 00:39:21 UTC
can u please explain me the difference between checked and unchecked exceptions also and is runtime exception and unchecked exceptions are both same??

thanks
Five answers:
dream
2012-10-05 00:42:40 UTC
An Error "indicates serious problems that a reasonable application should not try to catch."



An Exception "indicates conditions that a reasonable application might want to catch."



checked Exceptions must be dealt with in either a try/catch block or by

declaring a "throws" in a method. Unchecked exceptions normally are

Runtime exceptions like NullPointerException or ClassCastException.



A simple rule of thumb: If it's an exception you can possibly deal with

(continue to run the program using some alternative code), use checked

exceptions. For exceptions that should never happen (if they do, it's a

bug), use unchecked (Runtime) exceptions which will come up to the

surface and displayed to the user. Like this you assure that if there's

a bug, it will show up eventually and can be fixed, and you don't run

the risk of catching an exception and forgetting to deal with it (f.i.

empty catch block).
Tynach
2012-10-05 00:41:37 UTC
Which programming language(s) are you using?



Edit 1:



In regards to Java, they're a bit mixed. In general, an error will happen when you're trying to compile the program - it won't compile, so it won't even generate the bytecode for the JVM to run.



An exception happens if the program does compile correctly, but something goes bad as it is being run. In some languages, things like dividing by zero or having a bad pointer will be considered an 'error', but in Java these situations throw exceptions.



Exceptions are good because you can use try/catch to handle them, and let your program continue running or exit 'gracefully' (save any files that need saving, tell the user it's quitting and what went wrong, etc.).



If you run into a problem that doesn't throw an exception, but if it happens you want your program to handle it as if it were something going wrong, you can of course 'throw' your own exceptions, and even catch them. To do this, you derive from the exception class (java.lang.Exception).



Edit 2: I looked into the difference between checked and unchecked exceptions.



Unchecked exceptions are when you make a mistake. The program doesn't work properly, because you didn't do it right. This is often caused by silly things like swapping the inputs of a method, or even enormous things like forcing the entire control flow into a complicated series of loops without any method calls (everything being inline), and getting confused about which code is in which loop. I suppose deriving the wrong class can also count as this.



Checked exceptions are things that you can check for, and handle, on purpose. For example, if you make a text adventure game with some objects belonging to the player, and you somehow find yourself looking for an item in the player's inventory that isn't there, you can throw an exception that the item isn't in the player's inventory. That's something that can totally happen.



It's worth noting that in Java, unchecked exceptions derive from the RuntimeException class, whereas checked exceptions derive from plain Exception.
Techwing
2012-10-05 00:44:51 UTC
An error is a mistake made by the programmer in the source code. An exception is an unusual condition that arises at run time, detected by the operating system, the run-time support library, or the source code of a program. There is considerable overlap between these terms, so sometimes exceptions are also called errors, although errors are not normally called exceptions.



Checked exceptions are specific to certain programming languages, such as Java. Some languages require that any exceptions that will be generated ("thrown") by the program be declared in source code. This varies a lot by language so general definitions cannot be given.



Programmers who only know one or two programming languages may argue with the above, since they often believe that the whole world of programming works exactly like the one language that they happen to know.
villafane
2016-12-11 19:38:40 UTC
hi, Throwable has 2 subclasses a) Exception -> RuntimeException b) blunders. Runtime Exceptions could be dealt with via you. There are 2 sort i.e Checked and Unchecked. Compiler does not enable this technique to collect in case you do not handle Checked Exceptions ( eg. InterruptedException ), jointly as its your option to handle Unchecked Ones. in spite of the shown fact that, blunders won't have the ability to be dealt with via this technique in any respect ! Examples are OutOfMemory, DiskSpaceFull for which this technique can not do something in any respect !. The Gist right this is that RuntimeExceptions could be dealt with via this technique, jointly as blunders won't have the ability to be. wish this facilitates.
?
2012-10-05 00:54:19 UTC
Checked exceptions are exceptions that must be explicitly thrown. They are declared in the method signature and must be implemented by the caller.Non checked exceptions are exceptions that inherit from RuntimeException. So they are the same in that they share the same base class, so one RuntimeException handler could handle any exception that was thrown by one of the derived exception subclasses.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...