Search⌘ K

Checked vs Unchecked

Explore the distinctions between checked and unchecked exceptions in Java. Understand how methods handle these exceptions, why some require explicit declarations while others do not, and learn the implications for error handling in your code.

We'll cover the following...
1.

What are the different types or kinds of exceptions?

Show Answer
1 / 3
Java
class Demonstration {
public static void main( String args[] ) {
print("Code compiles without throws clause for unchecked exceptions.");
}
static void print(String str) {
if (str == null)
throw new RuntimeException("");
System.out.println(str);
}
}
1.

What are runtime exceptions?

Show Answer
Did you find this helpful?
Java
class Demonstration {
public static void main( String args[] ) {
printInt(null);
}
// throws NullPointerException
static void printInt(Number number) {
System.out.println(number.intValue());
}
}
1.

What are Error exceptions?

Show Answer
1 / 3
Java
class Demonstration {
public static void main( String args[] ) {
System.out.println( "Hello World!" );
}
// Doesn't compile because the checked exception being
// thrown is checked.
void uselesslMethod() {
throw new IOException();
}
}

Note if the method threw an Error or a ...