• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some questions regarding Exceptions.  RSS feed

 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have below questions regarding Exceptions:-

1). I have some general idea about 'Throws'. The way I see it, Java requires you to handle as many exceptions as possible - since it cannot predict runtime exceptions, it forces you to handle those which it can predict at compile time, and those are checked exceptions(I might be wrong in my understanding, in which case, I would be more than happy to stand corrected).
What I don't understand is this: We are required to declare exceptions using throws keyword, in cases where we 'anticipate' any exceptions. Is it really possible for the compiler(or any machine for that matter) to check for the remotest possibilities regarding certain events(in this case - exceptions)?(I do not have any experience in programming, so I have no idea)
In other words, how the compiler is able to predict that there is even a possibility of a method, throwing an exception?

2). Suppose I have a method which has a return type as int. It throws an exception. What will it return then, in that case? Suppose it is being called in such a way that its value is being assigned to another int variable, in another method. What will happen to the chain of execution as it throws an exception? How to deal with this situation gracefully?

3). Why is it not necessary to declare that a certain method throws exception, when try-catch is used?

4). Suppose I declare that certain method throws such and such exceptions. But I 'throw' them in an incorrect way - ie throw exceptions at normal/right conditions(when there is actually no exception) and do not throw anything at the conditions true for exceptions. Will the compiler be able to catch my mistakes in such a scenario?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:
In other words, how the compiler is able to predict that there is even a possibility of a method, throwing an exception?


The compiler is not predicting anything. It simply checks the code to see if (1) any checked exceptions are thrown (with the throw keyword), or (2) any methods that may be called, which are declared to throw a checked exception (with the throws keyword). If so, then those checked exceptions need to be handled, or the method itself must be declared to throw it.

There is no compile time logic to determine that the check exception will definitely be thrown. You can easily fool the compiler.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Bhatt wrote:
2). Suppose I have a method which has a return type as int. It throws an exception. What will it return then, in that case? Suppose it is being called in such a way that its value is being assigned to another int variable, in another method. What will happen to the chain of execution as it throws an exception? How to deal with this situation gracefully?


Nothing is returned. If the method (and assignment) is in the try block, then it will just jump to the appropriate catch block. If not, then the exception will continue upward, and be thrown out of that method, etc. etc. until it reaches an appropriate catch block, or completely unrolls the stack (where it will jump to the uncaught exception handler).

Henry
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3). Why is it not necessary to declare that a certain method throws exception, when try-catch is used?

Because you don't have to declare an exception when it's caught, only when it's thrown.  Catching an exception is the way you handle it.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4). Suppose I declare that certain method throws such and such exceptions. But I 'throw' them in an incorrect way - ie throw exceptions at normal/right conditions(when there is actually no exception) and do not throw anything at the conditions true for exceptions. Will the compiler be able to catch my mistakes in such a scenario? 

The compiler doesn't know what a "normal / right" condition is, it just knows that an exception has been thrown.

The compiler is not smart.  It will happily let you write this without warning:

We can see this will be a divide by zero exception, but the compiler can't.  If you wanted to catch the exception and continue or throw an exception when the division succeeds, the compiler will let you.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

Nothing is returned. If the method (and assignment) is in the try block, then it will just jump to the appropriate catch block. If not, then the exception will continue upward, and be thrown out of that method, etc. etc. until it reaches an appropriate catch block, or completely unrolls the stack (where it will jump to the uncaught exception handler).

Henry


Thanks! First, let me ask that can we make a return statement for a method(which is partially or fully, in the try block), in the catch block?(just in case the exception is thrown before reaching the original return statement in that method)

Second, what if I write nothing in the catch block? What will then happen in the case I originally asked above? The exception will not continue upward as I have already handled that in the catch block. So, what will get assigned to that int variable?

Third, what will happen if I rethrow a runtime exception in the catch block? How will the execution move forward then?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let me ask that can we make a return statement for a method(which is partially or fully, in the try block), in the catch block?(just in case the exception is thrown before reaching the original return statement in that method) 

It's legal to have a return statement in the catch block, but I'm not sure you need it for the reason you stated.   Consider this code:

The method divideOrZero() will always return c, either as the quotient of a and b, or zero if an exception is caught.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second, what if I write nothing in the catch block? What will then happen in the case I originally asked above? The exception will not continue upward as I have already handled that in the catch block. So, what will get assigned to that int variable?

Why not write a small program and find out?  (Or modify my program above.)
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Third, what will happen if I rethrow a runtime exception in the catch block? How will the execution move forward then?

Execute will halt with a runtime exception.  Only the try block catches exceptions.  Try it out in a program of your own.
 
Abhay Bhatt
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
First, let me ask that can we make a return statement for a method(which is partially or fully, in the try block), in the catch block?(just in case the exception is thrown before reaching the original return statement in that method) 

It's legal to have a return statement in the catch block, but I'm not sure you need it for the reason you stated.   Consider this code:

The method divideOrZero() will always return c, either as the quotient of a and b, or zero if an exception is caught.


Thanks, but I meant the case where original 'return' is inside try block. Sorry for not mentioning that explicitly.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think for a short method, the following would be acceptable.  Others would rather see just one return statement at the end.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!