Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 22nd, 2007, 06:04 PM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default throw an exception in catch

Hello.
I don't really understand what happens when you throw an exception inside a catch block. What I have understood is that the exception is sent to the method in which the exception that was caught occurs Like:

Code:
try {
  aMethod();      //In this method exception1 occurs
} catch(Exception1 e) {
  throw new Exception2();
}
Does Exception2 get sent to aMethod? If so what happens next? What if the same exception Exception1 is thrown in that catch block and is sent to aMethod again? Will it be caught again by the same catch block and end up repeating an infinite number of iterations?

I got a bit confused with this part of chapter 7. Any help will be apreciated.




Remember, remember the 5th of November. The gunpowder, treason, and plot. I know of no reason why the gunpowder treason should ever be forgot.
 
Old September 23rd, 2007, 11:37 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

say you've a code block like the one bellow
Code:
method1() throws Exception2{
   try {
     aMethod();      //In this method exception1 occurs
   } catch(Exception1 e) {
     // do some operations when exception occures
     throw new Exception2();
   }
}
method2() {
   try {
      method1();
   } catch(Exception2 e) {
      // handle the exception
   }
}

method1() throws an exception of type Exception2 you can see the method has a "throws" clause. If some other method calls this one that method [in this case method1()] should handle the exception.

So the throws new Exception2() wont call the same method it'll throw an exception and the control will be take out of the method where the exception occurred. If you the calling method [method2() in this example] did not handles it then the exception will be thrown all the way to JRE.
Hope its clear to you.


- Rakesh
 
Old September 24th, 2007, 06:42 AM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All clear.

Thank you Sir.

Remember, remember the 5th of November. The gunpowder, treason, and plot. I know of no reason why the gunpowder treason should ever be forgot.
 
Old April 11th, 2009, 10:56 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

The exception that you throw will be caught by the caller.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Try...Catch lowell VB.NET 3 July 23rd, 2007 06:35 AM
Try catch exception ayazhoda VB.NET 2002/2003 Basics 4 May 10th, 2007 10:27 AM
Ch 2 pg 70: links throw error subgenius BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 4 August 16th, 2004 05:26 PM
TRY CATCH THROW question savoym C# 1 July 1st, 2004 03:54 PM
how to pass multiple values throw url class object kfarooq Servlets 1 June 20th, 2003 04:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.