• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused with the definition of exception  RSS feed

 
nawab wajid
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if exception is an object which is thrown at RUN TIME then how do we have compiler checked exception.
answering this question can seriously remove my doubts.And what is the difference between unckecked exception and an error..??? thank you very much in advance
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of these should be answered if you work through the relevant chapter of the Oracle Java Tutorial (which you should bookmark and consult frequently in general): https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
 
nawab wajid
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot.... your reply was really helpful.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a newbie myself but here's what i know:
Whenever some class or method throws an exception you have to handle it. When you don't handle the compiler panicks (sort-of) and gives you an unchecked exception error.
Eventhough the exceptions are thrown at run-time the compiler needs to know (and it comes to know at compile time) that when that happens there are methods to handle it.
An error is nothing but a mistake. An error can be of many different kinds like syntactic errors,logical errors. The compiler shows the error report only for the syntactic errors and not for logical errors.
Hope I helped
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The are three types of things that get "thrown", which are all subclasses of java.lang.Throwable, and roughly speaking the purposes are these (there may be examples that don't quite fit):

- Checked exceptions (java.lang.Exception and subclasses that aren't a java.lang.RuntimeException). These are meant to be things that, as the programmer, you don't have control over. For example, you might be trying to connect to a server that is not available. So they indicate things you really do need to handle, which is why the compiler makes you.

- Unchecked exceptions (java.lang.RuntimeException and subclasses). These are things that you do have control over, and probably indicate an error in your code. E.g. NullPointerException - you should have either made sure the reference cannot be null, or checked for the condition first. It's perfectly reasonable to not catch these because you want to find out there's a bug and fix it, but often applications will probably catch it so they can log the error somewhere if nothing else. But the compiler won't make you do anything.

- Errors (java.lang.Error and subclasses). These indicate that something has gone seriously wrong that you can't realistically be expected to recover from. E.g. running out of memory. As such most applications shouldn't bother catching them, because there's not much they can do.
 
nawab wajid
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your replies removed all my doubts about the exceptions and i came to know the exact difference between the checked ,uncheckd and error... thanks a lot
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote: . . .
An error is nothing but a mistake. An error can be of many different kinds like syntactic errors,logical errors. The compiler shows the error report only for the syntactic errors and not for logical errors.
Hope I helped
Correct. Very important to realise that errors result from mistakes made in programming. The idea behind generics, the @Override annotation and similar is to enable the compiler to find a few logic errors too.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote: . . .
Whenever some class or method throws an exception you have to handle it. When you don't handle the compiler panicks (sort-of) and gives you an unchecked exception error.
Eventhough the exceptions are thrown at run-time the compiler needs to know (and it comes to know at compile time) that when that happens there are methods to handle it.
. . .
Afraid not.

You do not have to handle all Exceptions; for example a NullPointerException should be handled by correcting the source code and eliminating the null. You should not usually catch such an Exception but permit it to propagate.
The compiler does not panic. It is programmed to look for as many errors as possible. If for example you use the @Override annotation or generics, you are permitting the compiler to find logic errors which would otherwise go unnoticed. Remember the logic error is by far the worst kind; it is much more serious to produce a “wrong result” than have the program crash. The compiler is programmed to give error messages which enable the user to correct the errors, but writing error messages is a very difficult task, so they are not always right.

The theory of RuntimeExceptions is not that they occur at runtime. All Exceptions occur at runtime, so it is a confusing name. If you have a FileNotFoundException for example, the problem may lie in the file, and that is not part of the Java® runtime. So part of the exception occurs outside the runtime, so it is a checked exception. (At least that is the theory.) If you have an ArithmeticException, however, the division by 0 or whatever occurs entirely inside the Java® runtime. So it is a RuntimeException, and not checked because you need to correct the source to prevent the 0 being used as a divisor. The theory is that unchecked exceptions cannot be sorted out without correcting the code and checked exceptions can be sorted out by trying again after entering a new file name for example.
But if you look at things like NumberFormatException, maybe you can correct them by entering a new input, so the theory about Exceptions breaks down quite quickly.
Somebody posted a Java Tutorials link earlier in this thread. Read the whole “trail” which is a good introduction and explanation about Exceptions. Also try googling for “checked exceptions: an experiment which failed,” which will give you a completely different opinion.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean error or Error, NW?
An Error is a kind of Throwable (included with exceptions) which might prevent the runtime from running, and which cannot be corrected. An error means something incorrect.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa! Looks like I have lots of reading to do. Thanks for the help. I appreciate it.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, reading a lot will help you understand programming. You can probably read all of the links mentioned in an hour. And, “you're welcome”
 
nawab wajid
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow!!! ..nice explanation...all i understood about exceptions from this discussion is that checked exceptions are those over which we dont have control eg FileNotFoundException , here file which we are intersted in may not be present so part of this exception occurs outside the runtime but the FileNotFoundException object will still be created and thrown at runtime..the compiler just makes a check to see whether programmer has provided the mechanism to handle it if this exception occurs so that compiler relaxes. ..and unchecked exceptions are those which are caused due to programming problem and we do have a control over them. we can go back to our program and correct it .. hope i have understood correctly ..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!