• Post Reply Bookmark Topic Watch Topic
  • New Topic

catching main exception  RSS feed

 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

this question is just out of curiosity.
Please help.

The program code is:

class WithoutMain
{
static
{
try
{
System.out.println("I AM WITH OUT MAIN");
}
catch(Exception e)
{
System.out.println("Ohhhhhhhh");
}
}
}





Exception in thread "main" java.lang.NoSuchMethodError: main.

can this exception be caught somehow?

Please reply.

Thanks in advance.
 
Suraj Jadhav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The (run time)exception is thrown by JVM while executing the java program as you don't have main method in your code. You will get more on checked/unchecked exception from http://www.javaworld.com/javaworld/javaqa/2002-02/01-qa-0208-exceptional.html?page=1. Hope this will help.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Thread.setUncaughtExceptionHandler, available since Java 5.0.

But you shouldn't use your code this way. That class is clearly meant to be executed, so it should have a main method. All other tricks (calling System.exit in the static block, using a Thread.setUncaughtExceptionHandler) are just that - tricks to mask the real problem. The reason I mentioned the above method is therefore only academic. If you were on my team and I ever saw you use it in this way I would have you kicked off the team without any delay.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!