• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception?

 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define An Exception Called "NoMatchException" That Is Thrown When A String Is Not Equal To "Symbiosis". Write A Java Program That Uses This Exception?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds straight-forward enough. What's the problem?
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why we have used "printStacktrace()" in this program.....???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To print the stack trace.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

are you aware of stack trace ?
Whenever a thread calls a method it put it into its stack and when an Exception get thrown it unwind this stack until Exception get caught or until thread dies. this stack trace is useful to find out where exactly problem lies. just run your program with some other String "Symbiosis" and you will know why need to print stack trace.
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:
are you aware of stack trace ?
Whenever a thread calls a method it put it into its stack and when an Exception get thrown it unwind this stack until Exception get caught or until thread dies. this stack trace is useful to find out where exactly problem lies. just run your program with some other String "Symbiosis" and you will know why need to print stack trace.


in the above program we have made a "comparestring" method and passed a string parameter to it and now if the string str matched with "Symbiosis" it prints string are equal otherwise it throws a exception. now in main method we have called "comparestring" with "arg[0]" as argument......how we have used arg[0] to match it with "Symbiosis" and please explain the below code....according to me we have made a "nomatchexception" class and extends exception class....
then we have declared a nomatchexception method and passed strind parameter to it and then why we have super(meaasge) here......i know super is used to call the parent class...but here what message it will print here.....???



also if i am wrong somewhere..please tell....
thank you so much for your previous reply.....
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong in your code related to Creating Exception and using it.

you need to understand the usage of Exception , when an Exception thrown it indicates an error and to resolve that error you need to find Why error occurred and at which line of code error occurs ? here printstacktrace() comes in picture it will tell you at which line of your code error has occurred i.e. from which line Exception gets thrown .

Just run your program with input String as "Javin" and see what output comes. as per your code it will throw an Exception "nomatchexception" if you comment this line Exception will be thrown but stacktrace will not be printed.

I hope this help you.

Javin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic