This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Exception doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If catch block is always needed to handle exception either we declare exception or not, then what is the meaning to declare exception.I don't find any need to declare exception ?

This is written in Book->K&B6-> Page->372


Remember this:

Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception.

This rule is referred to as Java's "handle or declare" requirement. (Sometimes called "catch or declare.")



I am not getting how this is stated "handle or declare " .Exception handling is complete solution in itself.Does exception declaration is a complete solution in itself like exception handling? How they stated that declaration is alternative solution of handling ?("handle or declare" ).If i don't handle and just declare exception means problem is solved ??, then there should be no need of handling because i declared ?.

and another thing if i know particular method can throw particular exception then why i should declare it, why don't i write handler for that exception as i know about that, ultimately i have to write handler for that exception, so what magic declaration will do here.


I am not getting, what is the role and need of exception declaration here ,please satisfy me thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions in Java seems to answer your question.

Regards,
Dan
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you declare an exception you may do this:


You may not need to handle the IOException inside this myMethod.
You may need to handle it like this:
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Helen Ma wrote:When you declare an exception you may do this:


You may not need to handle the IOException inside this myMethod.
You may need to handle it like this:



Even without declaring it will work.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

Helen Ma wrote:When you declare an exception you may do this:


You may not need to handle the IOException inside this myMethod.
You may need to handle it like this:



Even without declaring it will work.




no it will not work without declaring it. IOException is a checked exception so it must honor java catch or specify requirement. either catch and handle it there and then or specify it to be thrown.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:

saloni jhanwar wrote:

Helen Ma wrote:When you declare an exception you may do this:


You may not need to handle the IOException inside this myMethod.
You may need to handle it like this:



Even without declaring it will work.




no it will not work without declaring it. IOException is a checked exception so it must honor java catch or specify requirement. either catch and handle it there and then or specify it to be thrown.



You didn't get, what i said and what i am asking ,read previous story.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
who says that without exception declaration "exception propagation" wont work and where is written this ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not about exception propagation . exception will propagate if it has not been handled where it is thrown. but i'm talking about the catch specify requirement. if your method is throwing a CHECKED exception, then it is an integral part of that method interface. you must either catch that exception or declare it with throws clause. if you don't declare it or handle it , it will be a compiler error. if however you honor the above catch and specify requirement, say you declare the exception in your method and exception occurs it will be propagated down in the method stack. if you haven't declared it but handled it , then it will be handled there and won't be propagated.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:it is not about exception propagation . exception will propagate if it has not been handled where it is thrown. but i'm talking about the catch specify requirement. if your method is throwing a CHECKED exception, then it is an integral part of that method interface. you must either catch that exception or declare it with throws clause. if you don't declare it or handle it , it will be a compiler error. if however you honor the above catch and specify requirement, say you declare the exception in your method and exception occurs it will be propagated down in the method stack. if you haven't declared it but handled it , then it will be handled there and won't be propagated.



Yes, that's why i asked my question was

If catch block is always needed to handle exception either we declare exception or not, then what is the meaning to declare exception.I don't find any need to declare exception ?

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If catch block is always needed to handle exception either we declare exception or not, then what is the meaning to declare exception.I don't find any need to declare exception ?


So that the exception handler would know which exception to catch. E.g. some File related APIs explicitly throw FileNotFoundException, or IOException etc. So, when you invoke those methods from your method, you would know that which exception to catch.

Similarly, when you are writing your own classes related to file operation, then your customized method could further declare your own custom made exceptions - one benefit of this approach is - when the parent method calls these methods, it(parent) knows which exceptions are expected, and which exceptions are really to worry about - e.g. if your file operations related method is throwing some JDBC related exception, then there's either problem in design, or - well, something is wrong (because ideally, the method should expect and throw exception which IS-A IOException).

Doing catch(Exception e) every time is not a good idea.

I hope this helps.
 
Ranch Hand
Posts: 450
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Most applications would have multiple layers/tiers. Layer/tier comparison

Exceptions should be handled in the layer that is best equipped to handle it correctly and if possible revert to a previous state or redo the operation without leading to an error.

Any Exception ought to be propagated until the layer which is intelligent enough handles it in the acceptable fashion.

Hope I am I able to convey my thoughts.

Cheers,
Raj.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

If catch block is always needed to handle exception either we declare exception or not, then what is the meaning to declare exception.I don't find any need to declare exception ?


So that the exception handler would know which exception to catch. E.g. some File related APIs explicitly throw FileNotFoundException, or IOException etc. So, when you invoke those methods from your method, you would know that which exception to catch.

Similarly, when you are writing your own classes related to file operation, then your customized method could further declare your own custom made exceptions - one benefit of this approach is - when the parent method calls these methods, it(parent) knows which exceptions are expected, and which exceptions are really to worry about - e.g. if your file operations related method is throwing some JDBC related exception, then there's either problem in design, or - well, something is wrong (because ideally, the method should expect and throw exception which IS-A IOException).

Doing catch(Exception e) every time is not a good idea.

I hope this helps.



This is written in Book->K&B6-> Page->372

Remember this:

Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception.

This rule is referred to as Java's "handle or declare" requirement. (Sometimes called "catch or declare.")



I am not getting how this is stated "handle or declare " .Exception handling is complete solution in itself.Does exception declaration is a complete solution in itself like exception handling? How they stated that declaration is alternative solution of handling ?("handle or declare" ).If i don't handle and just declare exception means problem is solved ??, then there should be no need of handling because i declared ?.

and another thing if i know particular method can throw particular exception then why i should declare it, why don't i write handler for that exception as i know about that, ultimately i have to write handler for that exception, so what magic declaration will do here.


I am not getting, what is the role and need of exception declaration here ,please satisfy me thanks.
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that might throws some exception must be either handled (try & catch) or declared (throws). In the second part i.e declaration part, it the duty of the caller method to handle this exception. If the caller method also declares, the one calling the calling method must handle the error. If no one handles but only declares, the stack may blow since what is thrown needs to be catched.

These links might help you

What is exception

Specifying the Exceptions Thrown by a Method

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumit Patil wrote:The code that might throws some exception must be either handled (try & catch) or declared (throws). In the second part i.e declaration part, it the duty of the caller method to handle this exception. If the caller method also declares, the one calling the calling method must handle the error. If no one handles but only declares, the stack may blow since what is thrown needs to be catched.

These links might help you

What is exception

Specifying the Exceptions Thrown by a Method



This is not answer of my question, even without exception declaration caller method can handle it if it has handler for that.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not getting how this is stated "handle or declare " .Exception handling is complete solution in itself.Does exception declaration is a complete solution in itself like exception handling? How they stated that declaration is alternative solution of handling ?("handle or declare" ).If i don't handle and just declare exception means problem is solved ??, then there should be no need of handling because i declared ?.

and another thing if i know particular method can throw particular exception then why i should declare it, why don't i write handler for that exception as i know about that, ultimately i have to write handler for that exception, so what magic declaration will do here.


I am not getting, what is the role and need of exception declaration here ,please satisfy me thanks.


Well, your questions are absolutely sensible from application developer's perspective. But consider the scenario in which you are simply writing a set of APIs, and those APIs will be invoked by some other developer(i.e. some other developer will use the jar of the code you wrote).

E.g. (again, I'm giving same example) You are writing a set of APIs which does some specific operations on file. A method (which you are writing) takes a File as an input argument, and does some stuff on it. Now, what if that file does not exist at all? What are you gonna do? Swallow the exception? Print stack trace? Or log it with severity ERROR or FATAL? Also, why you will be doing that? E.g. you are printing a stack trace, but the invoker doesn't want it - he/she wants to log the exception, but he/she can't do that, because he/she is simply using your jar file and does not have access to source code.

What I'm trying to say is - your job is simply to process the file. If file does not exist, then it is not your problem (because somebody else is providing File object, and it is his/her duty to make sure that it is a valid File). So, what you'll be doing is - simply declare your method as 'throws FileNotFoundException', and while opening the file, if you encounter that exception, just throw it further. Now, it becomes invoker's duty to handle it. Let the developer who calls your API handle the exception (i.e. print stack trace, or log the exception or whatever).

Yes, by declaring, you are not solving the problem, but you are forcing the invoker to catch the exception, and you are also giving the invoker the freedom to handle it in any way he/she wants. It can happen that invoker can further throw it, or wrap it in another checked exception and then throw it - it is totally his/her business.

I hope this helps.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

saloni jhanwar wrote:even without exception declaration caller method can handle it if it has handler for that.


Yes. But with declaration, the method forces caller method to handle the exception (otherwise, there will be a compile time error).
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

saloni jhanwar wrote:
This is not answer of my question, even without exception declaration caller method can handle it if it has handler for that.



What if there is no handler present to catch the exception?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

I am not getting how this is stated "handle or declare " .Exception handling is complete solution in itself.Does exception declaration is a complete solution in itself like exception handling? How they stated that declaration is alternative solution of handling ?("handle or declare" ).If i don't handle and just declare exception means problem is solved ??, then there should be no need of handling because i declared ?.

and another thing if i know particular method can throw particular exception then why i should declare it, why don't i write handler for that exception as i know about that, ultimately i have to write handler for that exception, so what magic declaration will do here.


I am not getting, what is the role and need of exception declaration here ,please satisfy me thanks.


Well, your questions are absolutely sensible from application developer's perspective. But consider the scenario in which you are simply writing a set of APIs, and those APIs will be invoked by some other developer(i.e. some other developer will use the jar of the code you wrote).

E.g. (again, I'm giving same example) You are writing a set of APIs which does some specific operations on file. A method (which you are writing) takes a File as an input argument, and does some stuff on it. Now, what if that file does not exist at all? What are you gonna do? Swallow the exception? Print stack trace? Or log it with severity ERROR or FATAL? Also, why you will be doing that? E.g. you are printing a stack trace, but the invoker doesn't want it - he/she wants to log the exception, but he/she can't do that, because he/she is simply using your jar file and does not have access to source code.

What I'm trying to say is - your job is simply to process the file. If file does not exist, then it is not your problem (because somebody else is providing File object, and it is his/her duty to make sure that it is a valid File). So, what you'll be doing is - simply declare your method as 'throws FileNotFoundException', and while opening the file, if you encounter that exception, just throw it further. Now, it becomes invoker's duty to handle it. Let the developer who calls your API handle the exception (i.e. print stack trace, or log the exception or whatever).

Yes, by declaring, you are not solving the problem, but you are forcing the invoker to catch the exception, and you are also giving the invoker the freedom to handle it in any way he/she wants. It can happen that invoker can further throw it, or wrap it in another checked exception and then throw it - it is totally his/her business.

I hope this helps.



Ok as if i am getting correct here that exception declaration is just an acknowledgement for another developer, practically it makes no difference either we declare or not. but still what you will say about statement "handle or declare", i don't think they are comparable so it stated like "handle or declare" .It gives nothing then confusion that declaration is a complete alternative solution of handling.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumit Patil wrote:

saloni jhanwar wrote:
This is not answer of my question, even without exception declaration caller method can handle it if it has handler for that.



What if there is no handler present to catch the exception?



Then "Exception propagation" will come in picture and it works fine in both condition either exception is declared or not.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

saloni jhanwar wrote:but still what you will say about statement "handle or declare", i don't think they are comparable


Yes. From functionality perspective, those are not equivalent of each other - handling is different than declaring. But I guess this 'handle or declare' term is used in context of compilation errors.
e.g. If a method A throws FileNotFoundException, and you are calling that method from method B, then for a successful compilation, method B should either 'handle or declare' FileNotFoundException.

I hope this helps.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My thought is that the book is specifically written for the exam and the statement "handle or declare" is written on the Java Compiler point of view. You either handle or declare checked Exceptions. You don't need to take that as good coding practices.

practically it makes no difference either we declare or not...
Then "Exception propagation" will come in picture and it works fine in both condition either exception is declared or not.


Nope. If you don't declare, you cannot propagate a checked Exception using throw clause.

Anayonkar beat me
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Jai wrote:My thought is that the book is specifically written for the exam and the statement "handle or declare" is written on the Java Compiler point of view. You either handle or declare checked Exceptions. You don't need to take that as good coding practices.

practically it makes no difference either we declare or not...
Then "Exception propagation" will come in picture and it works fine in both condition either exception is declared or not.


Nope. If you don't declare, you cannot propagate a checked Exception using throw clause.

Anayonkar beat me



see this,without declaration propagation is working here, and this is an exercise of propagation.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

see this,without declaration propagation is working here, and this is an exercise of propagation.


That is because IllegalArgumentException IS-A RuntimeException and

"A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught."

(the part in quotes is from code comments of java.lang.RuntimeException).
If you replace that IllegalArgumentException by, say, FileNotFoundException, then you'll get what John Jai is trying to say.

I hope this helps.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

see this,without declaration propagation is working here, and this is an exercise of propagation.


That is because IllegalArgumentException IS-A RuntimeException and

"A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught."

(the part in quotes is from code comments of java.lang.RuntimeException).
If you replace that IllegalArgumentException by, say, FileNotFoundException, then you'll get what John Jai is trying to say.

I hope this helps.



That means K&B selected bad exercise on propagation
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.

Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method

Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked.

With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method.

Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be, as they tend to be unrecoverable.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!