• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

java exceptions

 
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".
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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!!!
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • 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 Tomcat Server Java
  • 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
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
start from here
 
It's a tiny ad only because the water is so cold.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic