• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Exception

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question was obtained from a Whizlab's Test.
-----
Which line can be inserted at line 6 to ensure that the method
throws a CustomException when an exception occurs in a try block and that the CustomException also contains the original exception that is thrown from within the try block?
Note: For the main method in the original code (below), it did not handle the Exception and was missing the throws clause.

Before



A. throw e;
B. throw new Exception();
C. throw new CustomException();
D. throw new CustomException(e);

Answer : D
The exception that occurs within the try block must be wrapped within CustomException.
It can be set as a cause by passing the original exception through the constructor of CustomException.
It can also occur after the no-arg constructor is called by calling the initCause method.
------


For the solution of the test question, the exception thrown contains a parameter of type Exception.



Question1:
Is a characteristic of a "Custom Exception" in Java to use a
1) String or
2) Exception object
as a parameter?

Question2: Is answer D correct because it contains the object 'e' as a parameter?

After:



I believe


is related to


Please confirm.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:Note: For the main method in the original code (below), it did not handle the Exception and was missing the throws clause.

That's a (major) mistake in their question and should definitely be updated. Checked exception must always be handled or declared. Well spotted!

Thomas Hauck wrote:Question1:
Is a characteristic of a "Custom Exception" in Java to use a
1) String or
2) Exception object
as a parameter?

It all depends on your needs. The Throwable, Exception, RuntimeException and Error classes have all 4 public constructors: one without parameters, one with a String (the detail message), one with a Throwable (the cause of the exception) and one with a String (the detail message) and a Throwable (the cause of the exception). So your custom exception can define any of them and just invoke the appropriate super constructor. But you can also add some own parameters if needed.


Thomas Hauck wrote:Question2: Is answer D correct because it contains the object 'e' as a parameter?

That's correct! The CustomException class defines 2 constructors: one without parameters and one with a Throwable. So you can create a CustomException instance in 2 ways: new CustomException() and new CustomException(someThrowable). The question requires to keep the original exception (the CustomException also contains the original exception that is thrown from within the try block). So that's only achievable with answer D. If you would have opted for answer C, you would have not met that requirement and the original exception (in this example an Exception instance) would have been lost. Both answers will result in a different stack trace when you run the little program. You should definitely try to run the program with both answers and have a look at the differences in the produced stack trace. It will clearly show the difference between both answers. That's a little bit of home work
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for your reply.



Here is the output using the 2 different types of custom exceptions.

Program output using line 1
Exception in thread "main" com.java.exceptions.CustomException: java.lang.Exception
at com.java.exceptions.Test.m1(Test.java:14)
at com.java.exceptions.Test.main(Test.java:20)
Caused by: java.lang.Exception
at com.java.exceptions.Test.m1(Test.java:11)
... 1 more


Program output using line 2

Exception in thread "main" com.java.exceptions.CustomException
at com.java.exceptions.Test.m1(Test.java:15)
at com.java.exceptions.Test.main(Test.java:20)
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the explanation of the very first posting of this thread, Whizlabs wrote after the answer it listed:

1) It can be set as a cause by passing the original exception through the constructor of CustomException.
2) It can also occur after the no-arg constructor is called by calling the initCause method.


I found the following method in "Java The Complete Reference" by Herb Schildt

Throwable initCause(Throwable e) :
Associates e with the invoking exception as a cause of the invoking exception.
Returns a reference to the exception.


For option 2) I have changed the code below and wanted to have confirmed that this change is correct and is what was meant by option 2) in the explanation.



For option 1)
"It can be set as a cause by passing the original exception through the constructor of CustomException."
I am not clear what is meant and how this would be implemented by means of Java code.

Could you please clarify.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the following CustomException:


Option 1: It can be set as a cause by passing the original exception through the constructor of CustomException.


Option 2: It can also occur after the no-arg constructor is called by calling the initCause method.


Hope it helps!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic