Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java exceptions

 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a class with a main( ) that throws an object of class Exception inside a try block.
Give the constructor for Exception a String argument.
Catch the exception inside a catch clause and print the String argument.
Add a finally clause and print a message to prove you were there.
Please give the solution for the same."Please explain how to use Exception constructor".
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

On JavaRanch, we help people to learn Java. The point of homework is that you learn Java by doing it yourself. You don't learn anything by letting other people do your homework. So, we are not going to give you the complete solution for this assignment just like that. Have you tried writing a program for this assignment already yourself? Show us your code. Explain in detail where exactly you get stuck, and we'll help you with that.

If your question is specifically about the sentence "Give the constructor for Exception a String argument": An Exception is just another class that you can create instances of using the new operator. You're supposed to create a new Exception object, passing a string to the constructor, and then throw the exception. Like this:
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:Create a class with a main( ) that throws an object of class Exception inside a try block.
Give the constructor for Exception a String argument.
Catch the exception inside a catch clause and print the String argument.
Add a finally clause and print a message to prove you were there.
Please give the solution for the same."Please explain how to use Exception constructor".




K.that was very nice on you to suggest me to do tge code by myself!!

public class FirstException
{
FirstException(String msg)
{
msg="this is bound to execute";
System.out.println(msg);
}
public static void main(String[] args)throws Exception
{
try
{
throw new Exception();
}
catch(Exception e)
{
e.printStackTrace();
}

finally
{
System.out.println("i will get printed");
}

}
}



I am not knowing the way to throw an object of class Exception inside a try block!!!Please help...Is it possible to throw an exception in try block??please suggest!!!
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it possible to throw an exception in try block


Its possible.
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tidake wrote:Hi,
Is it possible to throw an exception in try block


Its possible.




Is the use of code below done correctly?
FirstException(String msg) {
msg = "this is bound to execute";
System.out.println(msg);
}

Im am getting an error saying "cannot find symbol"!!

Please suggest the right way of using the constructor!!I m not just getting as to hw it works!!
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please suggest the right way of using the constructor!!I m not just getting as to hw it works!!


I/m not clear about your doubt.What are you trying to say?

1.How to use the constructor
2.How to write your own exception
3.How to throw an exception from try block.
4.How throw works in try catch block

Thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav wrote:K tge hw

Please UseRealWords
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tidake wrote:Hi,

Please suggest the right way of using the constructor!!I m not just getting as to hw it works!!


I/m not clear about your doubt.What are you trying to say?

1.How to use the constructor
2.How to write your own exception
3.How to throw an exception from try block.
4.How throw works in try catch block

Thanks


Its How to use the constructor and How to throw an exception from try block!!!Please help
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im am getting an error saying "cannot find symbol"!!

If you read the error text carefully, you will notice it is referring to the variable "msg"
What is msg? Have you defined it anywhere? (Hint)
 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
keshav wrote:K tge hw

Please UseRealWords



Yes!!I have defined "msg" in the Exception constructor as" FirstException(String msg){ String msg="this is bound to work";
System.out.println(msg);
} "
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote:
Yes!!I have defined "msg" in the Exception constructor as" FirstException(String msg){ String msg="this is bound to work";
System.out.println(msg);
} "

No you didn't. This is the code you posted before.

Also please UseCodeTags while posting code
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
start from here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic