• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Runtime Exception

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why Runtime Exceptions are Not Checked in java..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has two kinds of exceptions: checked and unchecked exceptions. According to the specification, java.lang.RuntimeException and all other exceptions that are subclasses of RuntimeException are unchecked; all other exceptions are checked.

That's just how it is defined and there's no asking "why".

If your question is really: "Why do we have a distinction between checked and unchecked exceptions in Java?": In software, there are generally two kinds of errors that can happen while a program is running: expected errors and unexpected errors.

An expected error is something that the programmer can expect to go wrong sometimes. An example of this is a network connection error. The computer might not be connected to the network when the program runs, and the program should handle such an error nicely, for example by showing an error message to the user.

An unexpected error happens when something goes wrong in the program unexpectedly, and it is almost always an indication that there's a bug in the program. As the programmer, you can't do much about that while the program is running - all you can do is debug the program afterwards and fix the error in your code.

Java checked and unchecked exceptions roughly compare to expected and unexpected errors. You are supposed to handle checked exceptions, and that's why the compiler checks if you handle them properly.

When you write your own exception classes, you should think about whether your exception represents an expected error or an unexpected error and subclass Exception or RuntimeException accordingly.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Becasue some exceptions are not known until the program is running.

Besides if all exceptions were checked at compile time, most of your code would have to be exception handling code.
 
jeff rusty
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked exception needs to be thrown or caught if you want your program to compile.
Although you also have the liberty to catch or throw unchecked exception like NullPointer Exception, but it would be a very bad programming practise and it is highly discouraged. Generally we use assert to test our application for unchecked exception.

Regards
Prem
 
reply
    Bookmark Topic Watch Topic
  • New Topic