Forums Register Login

Confused about exceptions

+Pie Number of slices to send: Send
A lot of books and tutorials say things along the lines of:
"The Java runtime system requires that a method either catch or specify all checked exceptions that can be thrown by that method"
But I can write classes without throwing or catching anything..... ?
+Pie Number of slices to send: Send
Yes you can write. But if your class using any of Java class methods which throws checked exceptions then you need to catch them or throw them back.
+Pie Number of slices to send: Send
The reason you can write classes and methods without worrying about exceptions is that you have not yet encountered an exception that must be handled. Try to compile the following piece of code:



and see what happens...
+Pie Number of slices to send: Send
Due to heavy use of checked exceptions and minimal use of unchecked exceptions, there has been a hot debate in the Java community regarding true value of checked exceptions. Use checked exceptions when the client code can take some useful recovery action based on information in exception. Use unchecked exception when client code cannot do anything. For example, convert your SQLException into another checked exception if the client code can recover from it and convert your SQLException into an unchecked (i.e. RuntimeException) exception, if the client code cannot do anything about it.
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 688 times.
Similar Threads
Reg. main method
why does this compile?
Can the main() method throw Exceptions ?
Regarding Overiding
Can Constructors throw Exceptions??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:46:48.