• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between implementation of custom exception and normal exception  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am being asked to throw a normal exception and a custom exception. I have created an exception . And i want to know what kind of exception have i created, whether normal exception or custom exception and how to create the other one.

Class Account:


Class InsufficientFunds:


   
Class Main :

 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have as many catch blocks as you want in your code. So you could have:

You can also throw a new exception in a catch block like:

Please note that you have to go from the most specific/narrow exception to the most generic/wide exception otherwise it will not compile.

It is also helpful to append the word Exception to your exception class. This will help debugging in the future/
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pete

Thanks.
I figured out .
Normal Exception is


Custom Exception is


Class insufficient excpetion
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The custom exception should call super with the text of the message to go with the exception.  Printing a message in the constructor would not pass the message to the printStackTrace() method.

The withdraw method should have a throws clause for the exception it throws.
The withdraw method should NOT catch the exception it throws. The idea is that the exception should be passed to the caller of the withdraw method.
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad that you got your problem solved @Sucheta.
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that 'working' and 'working correctly' are two different things and @Norm Radder brings up some really good points.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now in Insufficientfunds exception i have to throw a new message without using super. How will it work.




Here i have to throw a message without using insufficient funds super message hw will that work.



[/code]

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hw will that work.

What happens when the statement on line 4 (super) is not commented out?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already told you how to create your own exception classes.

The super(...); calls work exactly the same way they do in any other constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!