• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

checked and unchecked exceptions

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, can someone tell me what are checked exceptions and what are unchecked exceptions? Thanks.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked exceptions are exceptions that must be encased within a try/catch block or the compiler will complain. For example...
public static void main(String argv[])
{
RandomAccessFile r = new RandomAccessFile("iotest.txt","rw");
}
...would cause a compile time error. RandomAccessFile's constructor throws an IOException, and since IOException is not a subclass of RuntimeException, it is a checked exception. And since I'm not encasing it in a try/catch block, the compiler will complain.
The remedy is ...
public static void main(String argv[])
{
try {
RandomAccessFile r = new RandomAccessFile("iotest.txt","rw");
} catch (IOException e)
{
System.out.println("Checked Exception Caught");
}
}

On the other hand, the compiler doesn't check for runtime exceptions (unchecked exceptions). For example, since ArithmeticException is a subclass of RuntimeException, throwing it won't cause a compile time error.
For example...
public static void main(String argv[])
{
throw new ArithmeticException();
}
...would compile successful but give you an exception during runtime.
Hope that helped.

Jimmy Blakely,
Sun Certified Java Programmer
Travis County Certified Defensive Driver

[This message has been edited by Jimmy Blakely (edited August 29, 2001).]
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jimmy, I was wondering there is is a list of checked exceptions and unchecked exceptions. There are times I get confused as to what type of exception a particular exception is. I know ArithmaticException is a unchecked. So is ArrayOutOfBounds. But it's because I worked with them alot. Is there more?
 
Jimmy Blakely
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not too sure, and hopefully somebody will correct me if I'm wrong.
Try this....whenever the instanceof operator returns true, it is a runtime exception and NOT a checked exception.
boolean b = new ArithmeticException() instanceof RuntimeException;
System.out.println(b);
Using this snippet of code, I've found the following...
Unchecked Exceptions (Runtime)
ArithmeticException
IllegalArgumentException
IndexOutOfBoundsException
StringIndexOutOfBoundsException
SecurityException
IllegalMonitorStateException

Checked Exceptions (Compile Time)
IOException
FileNotFoundException
InterruptedException
Those are all I could think of at the moment. Just use that code snippet to test any exceptions I haven't included. I
Hope I was of assistance.
Jimmy Blakely,
Sun Certified Java Programmer
Travis County Certified Defensive Driver
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Cameron,
Jimmy is perfectly right.
RuntimeException and all its sub classes(including those which you made by extending RuntimeException class) are UNCHECKED exceptions.
IOException and all its sub classes(including those which you made by extending IOException or Exception class) are CHECKED exceptions.
Regards
Gurpreet Sachdeva
For Mock Exams and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jimmy and Gurpreet.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic