| Author |
java exceptions
|
keshav pradeep ramanath
Greenhorn
Joined: Aug 18, 2011
Posts: 5
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
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:
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
keshav pradeep ramanath
Greenhorn
Joined: Aug 18, 2011
Posts: 5
|
|
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
Joined: Nov 04, 2008
Posts: 146
|
|
Hi,
Is it possible to throw an exception in try block
Its possible.
|
The important thing is not to stop questioning.Curiosity has its own reason for existing.
|
 |
keshav pradeep ramanath
Greenhorn
Joined: Aug 18, 2011
Posts: 5
|
|
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
Joined: Nov 04, 2008
Posts: 146
|
|
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
Joined: Jul 26, 2007
Posts: 8441
|
|
keshav wrote:K tge hw
Please UseRealWords
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
keshav pradeep ramanath
Greenhorn
Joined: Aug 18, 2011
Posts: 5
|
|
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
Joined: Jul 26, 2007
Posts: 8441
|
|
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
Joined: Aug 18, 2011
Posts: 5
|
|
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
Joined: Jul 26, 2007
Posts: 8441
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
start from here
|
 |
 |
|
|
subject: java exceptions
|
|
|