6 Replies - 542 Views - Last Post: 01 April 2012 - 06:04 PM Rate Topic: -----

#1 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Clear up a few questions?

Posted 01 April 2012 - 02:27 PM

My professor wants us to answer a few questions using the textbook but I can't seem to figure some of them out. The first question:

How is the caller's flow of control (sequence of statement
execution) different when an exception is thrown from a method
that it calls, vs. when the method returns normally?


I'm not sure what the answer is, but wouldn't it disrupt the program by skipping the code in the try block since an unexpected error is thrown, whereas, the program runs through all the methods in the try if everything returns normally?

Is This A Good Question/Topic? 0
  • +

Replies To: Clear up a few questions?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: Clear up a few questions?

Posted 01 April 2012 - 02:29 PM

If I have the following snippet:
try{
   a();
   b();
   c();
}
catch(Exception e){

}



And b() throws an Exception, then c() will be skipped over and the catch block will be executed. The a() method will still be executed though.
Was This Post Helpful? 1
  • +
  • -

#3 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Clear up a few questions?

Posted 01 April 2012 - 02:34 PM

Ok so basically the difference is when a method in the try throws an exception, all methods after that method are skipped, and obviously if everything returns normally then the program runs through all the methods.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: Clear up a few questions?

Posted 01 April 2012 - 02:39 PM

Yes. That's basically it.
Was This Post Helpful? 0
  • +
  • -

#5 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Clear up a few questions?

Posted 01 April 2012 - 02:50 PM

I was having issues with one other question:

The following try/catch has code to explicitly handle any
IllegalArgumentException thrown. Show the modified code that
will also catch any other type of exception that could be thrown
by code called within the try block.

try
{
... methods called here ...
...
}
catch (IllegalArgumentException e)
{
... code to handle IllegalArgumentException ...
}


Well it looks to me that all that needs modification is the catch block. Since it should just catch all exceptions it shouldn't matter what methods are within the try. I saw this code posted in a tutorial for "the catch-any clause":

Quote

try
{
// ...
}
catch(Exception ex)
{
// Exception handling code for ANY exception
}
try
{
// ...
}
catch(Exception ex)
{
// Exception handling code for ANY exception
}


Is this all I would have to do? The only differene is withing the parentheses of the catch.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: Clear up a few questions?

Posted 01 April 2012 - 02:58 PM

Quote

Is this all I would have to do? The only differene is withing the parentheses of the catch.

Yes. Though you would honestly get your question answered faster if you wrote up a sample and ran it.
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Clear up a few questions?

Posted 01 April 2012 - 06:04 PM

You can always daisy chain exception

try {
   statement
}
catch(specific exception 1) {
   ....
}
catch(specific exception 2) {
   ....
}
catch(Exception e) {   // catch all Exceptiom
    ....
}
catch(Throwable t) {
    ..... catch even more
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1