• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Conceptual Question about Exception

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a simple question.

When writing and API, in which conditions should a function throw checked Exception and in which conditions it should throw an unchecked Exception?

Regards,
Kashif
 
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
Hmm, the question sounds simple, but there is no exact answer to it.

Note that checked exceptions are a feature that's unique to Java. Other programming languages do have exceptions, but do not make a distinction between checked and unchecked exceptions.

Some people think that checked exceptions are a failed experiment and that you shouldn't use them at all; see also Removing Checked Exceptions from Java. Others think that checked exceptions are a useful feature.

If you want a rule of thumb, then read this page from Sun's Java tutorials, it might give you some idea of what the different kinds of exceptions are supposed to be used for:

The Three Kinds of Exceptions

The first kind of exception is the checked exception. These are exceptional conditions that a well-written application should anticipate and recover from. For example, ...

The second kind of exception is the error. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, ...

The third kind of exception is the runtime exception. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API. For example, ...


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unchecked Exceptions are not necessary to handle or declare, but checked exception need to declare or handle.
While writing the API you don't need to bother about unchecked exceptions that means it doesn't make difference by throwing unchecked exception but if you want to make mandatory for calling function to handle or declare particular exception then you can throw checked exception.

-Abhishek
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic