• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Exception doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KB6 Page 377 doubt
I ain't getting this at all.

"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "


Problem is that i had read before this that
(1) If there is any exception encountered in try block then catch block will handle it and at the last finally block will execute
(2) If there is no exception encountered in try block then catch block will be ignored and at last only finally block will execute.

Now in this above statement K&B ,it says if i use finally then all catch blocks will be ignored ? what is this ?
 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you rethrow the exception you caught inside the catch block, all other catch clauses associated with the same try block is ignored.

This example will help you out

try {
//some code that throws Exception
} catch(IOException e){
throw e; //rethrowing the Exception caught
}
catch(Exception ex){
//this will not be executed all other catch clauses associated with the same try block is ignored.
}
finally{
System.out.println("This will be printed"); if a finally block exists, it runs
}

Hope it helps
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "



Hi,

Not all of us have the book. Please type the above with the proper period, comma, or whatever, so that we can understand the context. Only then can we help you correctly. I think there are some missing periods in the above, or even related to a previous sentence which you need to type to if necessary.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jesus Angeles.
Saloni jhanwar, post the complete matter and not the page number.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kunal Lakhani wrote:I agree with Jesus Angeles.
Saloni jhanwar, post the complete matter and not the page number.



Rethrowing the Same Exception
Just as you can throw a new exception from a catch clause, you can also throw the same exception you just caught. Here's a catch clause that does this:

catch(IOException e) {
// Do things, then if you decide you can't handle it...
throw e;
}

All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). If you throw a checked exception from a catch clause, you must also declare that exception! In other words, you must handle and declare, as opposed to handle or declare. The following example is illegal:

public void doStuff() {
try {
// risky IO things
} catch(IOException ex) {
// can't handle it
throw ex; // Can't throw it unless you declare it
}
}

In the preceding code, the dostuff() method is clearly able to throw a checked exception—in this case an IOException—so the compiler says, "Well, that's just peachy that you have a try/catch in there, but it's not good enough. If you might rethrow the IOException you catch, then you must declare it!"



Happy ???
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:KB6 Page 377 doubt
I ain't getting this at all.

"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "


Problem is that i had read before this that
(1) If there is any exception encountered in try block then catch block will handle it and at the last finally block will execute
(2) If there is no exception encountered in try block then catch block will be ignored and at last only finally block will execute.

Now in this above statement K&B ,it says if i use finally then all catch blocks will be ignored ? what is this ?



You are analyzing a sentence by itself. Read the topic according to its context. Just a sentence before that, the book says 'you can also throw the same exception you just caught.'. It is talking about rethrowing the same exception you caught. It happens after the normal catch clause catches the first exception thrown.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesus Angeles wrote:

saloni jhanwar wrote:KB6 Page 377 doubt
I ain't getting this at all.

"All other catch clauses associated with the same try are ignored, if a finally block exists, it runs, and the exception is thrown back to the calling method (the next method down the call stack). "


Problem is that i had read before this that
(1) If there is any exception encountered in try block then catch block will handle it and at the last finally block will execute
(2) If there is no exception encountered in try block then catch block will be ignored and at last only finally block will execute.

Now in this above statement K&B ,it says if i use finally then all catch blocks will be ignored ? what is this ?



You are analyzing a sentence by itself. Read the topic according to its context. Just a sentence before that, the book says 'you can also throw the same exception you just caught.'. It is talking about rethrowing the same exception you caught. It happens after the normal catch clause catches the first exception thrown.



I got it.Problem was some where else.I was reading it in different way that's why i got confused.I was reading "All other catch clauses associated with the same try are ignored, if a finally block exists" , i thought because of finally block all catch blocks will be ignored.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic