• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to catch Runtime error

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below shown are the 3 classes, I developed. When I run ElementTest.java, I cannot see the output. I am unable to put it in debug also. I am using JBuilder tool for editing, compling & running the code. I am expecting it to print the error message
"Trying to add duplicate Element" which was coded in file Element.java. But I think I am getting a runtime error. How do I know in which class I am getting that runtime error and what it is?
Thank U.
----------------- File Element.java --------------------------
package testexamples;
public class Element {
private String name;
public Element(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
----------------- File Set.java --------------------------
package testexamples;
import java.util.Vector;
public class Set {
private String name;
private Vector elements;
public Set(String name) {
this.name = name;
elements = new Vector();
}
public void addElement(Element el) {
if(! elements.contains( el )) {
elements.addElement(el);
}
else {
System.out.println(" Trying to add Duplicate Element:"+ el);
}
}
}
----------------- File ElementTest.java ----------------------
package testexamples;
import java.util.*;
import java.io.*;
public class ElementTest{
public ElementTest() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("log.txt");
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
PrintStream ps = new PrintStream(fos);
Element e1 = new Element("e1");
Element e2= new Element("e2");
Element e3= new Element("e3");
Element e4= new Element("e1");
Element e5= new Element("e2");
Set s1= new Set("s1");
Set s2= new Set("s2");
s1.addElement(e1);
s1.addElement(e2);
s1.addElement(e3);
s1.addElement(e4);
s1.addElement(e1);
s2.addElement(e1);
// s2.removeElement(e1);
s2.addElement(e1); // etc ...
// list of the elements in the set
ps.println("Here is the list of the elements");
ps.close();
}
public void main(String[] args) {
ElementTest elementTest = new ElementTest();
elementTest.invokedStandalone = true;
while(true);
}
private boolean invokedStandalone = false;
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Madhu Jannu:
Below shown are the 3 classes, I developed. When I run ElementTest.java, I cannot see the output. I am unable to put it in debug also. I am using JBuilder tool for editing, compling & running the code. I am expecting it to print the error message
"Trying to add duplicate Element" which was coded in file Element.java. But I think I am getting a runtime error. How do I know in which class I am getting that runtime error and what it is?
Thank U.
----------------- File Element.java



------------------
 
mukesh ahuja
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just have a look on ElementTest.java file it is not having a main () method with following signature i.e
public static void main(String args[]) it is missing static keyword that why your application is not working and throwing runtime exception.
mukesh

Originally posted by Madhu Jannu:
Below shown are the 3 classes, I developed. When I run ElementTest.java, I cannot see the output. I am unable to put it in debug also. I am using JBuilder tool for editing, compling & running the code. I am expecting it to print the error message
"Trying to add duplicate Element" which was coded in file Element.java. But I think I am getting a runtime error. How do I know in which class I am getting that runtime error and what it is?
Thank U.
----------------- File Element.java



------------------
 
Madhu Jannu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u Mukesh.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!