• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions - Please help!!  RSS feed

 
Neha Mittal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do we know whether an error in a given code will result in Compilation Error, or Runtime error or will throw an Exception(and that it'll be one thrown by JVM or the API)? Because there are times when a class compiles fine but an exception is thrown when some method is called. How to make out this?
[ December 10, 2008: Message edited by: Neha Mittal ]
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should check out the Sun documentation, the tuturials and trails are very helpful and they have one on Exceptions:
Exceptions Trail
Also a search of this forum will likely turn up several results that would help you.

Generally speaking there are two types of Exception, checked and unchecked.
Unchecked exceptions are exceptions that are generally throw at runtime, out of memory etc.
Unchecked exceptions need not be handled by your code, there isnt really going to be anything you can do to recover from them.

Checked Exceptions on the otherhand must be caught by your code, or they propgate up to the JVM (This could be a valid use case)
Generally you can either catch the exception using a try/catch block, or declare your method to throw an exception of required type:



Basicly as myObject is null you cant call any of its methods, so when you attempt to a NullPointerException is thrown, you have to catch this or propergate it up to the calling method.

I do recommand you read the trail i linked in, it gives some good advice on the use of Exceptions.

G

[ December 10, 2008: Message edited by: Gavin Tranter ]
[ December 10, 2008: Message edited by: Gavin Tranter ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!