• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Exception declaration

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, What is the difference between Line # 7 and Line # 13 . What I mean is, When should we use throw new <some exception> and when should we use throw <exception reference variable>

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It should be used when the programmer wants to throw the Exception for the first time

It's used to rethrow a Exception

If Exception was an Checked Exception ,you have declare
with the main method


Refer KB BOOK for more on Exceptions
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi, What is the difference between Line # 7 and Line # 13 . What I mean is, When should we use throw new <some exception> and when should we use throw <exception reference variable>




In a catch block you can throw a new "Exception"

or you can rethrow the catched one.



The first case "reset" the stack trace
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:


line1 : This line creates a new exception and throws that
now when exception is thrown and then it is caught in the catch block, it is being referred by variable e

line 3: here we are throwing just the exception object that is caught in the catch block
we are not creating a new exception here

hope this helps
happy to help
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,

1. What is the use of catching the created exception at Line 10 and re-throwing at Line 13. We can get the similar/same behavior with throws concept know ?
2. Coming to syntax for throw, Do we have only 2 options throw new <some exception> and throw <exception reference variable> . Please advice if there is another way to create/use throw syntax.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad/Simon, Could you please answer for this of you have any idea.

Harikrishna Gorrepati wrote:So,

1. What is the use of catching the created exception at Line 10 and re-throwing at Line 13. We can get the similar/same behavior with throws concept know ?
2. Coming to syntax for throw, Do we have only 2 options throw new <some exception> and throw <exception reference variable> . Please advice if there is another way to create/use throw syntax.

 
Saloon Keeper
Posts: 10300
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You catch and re-throw an Exception if you want to do something (like print a String) when you catch an exception, but you still want it to propagate up the stack.

Throw a new exception if either there is no earlier exception to re-throw, or if you catch an exception, and the exception is not appropriate for the client of your class.

For instance, let's say you have a car:
If you have a client that makes use of your car, throwing a technical exception might not really be interesting or appropriate. In that case you can throw a more appropriate exception, and provide the original exception as a cause of the problem.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan. Your explanation is awesome.
 
Stephan van Hulst
Saloon Keeper
Posts: 10300
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LAst question on this. Point #2. Coming to syntax for throw, Do we have only 2 options throw new <some exception> and throw <exception reference variable> . Please advice if there is another way to create/use throw syntax.
 
Stephan van Hulst
Saloon Keeper
Posts: 10300
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a different syntax. It's the same syntax. In both cases you are throwing an instance of a subclass of Throwable. The fact that in one case the instance is instantiated on the spot, doesn't change that it's the same syntax.

An analogy:

The syntax for assigning an instance to a reference is as such:

variable = instance;

The fact that instance can be an already existing instance, or an instance you create by calling new Instance(), does not change the syntax. The same goes for throws:

throw throwable;

throwable may be an existing instance, or it can be newly created.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!