Following exceptions are Checked Exception or Unchecked Exception


Recommended Answers

All 10 Replies

ArrayStoreException extends RuntimeException, so it's unchecked
IllegalArgumentException extends RuntimeException, so it's unchecked

Simply.... if it extends RuntimeException or Error it's unchecked, otherwize it's checked.

The unchecked exception classes are the run-time exception classes and the error classes.The checked exception classes are all exception classes other than the unchecked exception classes.

Java Language Spec 11.1.1 The Kinds of Exceptions

but when illegal argument occured in program then program does not compile why?????????????????

Without seeing the program it's impossible to say.
Post the code and the error message

code is following

class Run
    int b;
    public void get(int a)
class ArgumentException

    public static void main(String s[])

        Run ob=new Run();

and error message error: method get in class Run cannot be applied to g
iven types;
  required: int
  found: double
  reason: actual argument double cannot be converted to int by method invocation
1 error

That has absolutely NOTHING to do with Exceptions.
The method needs an int parameter, and you passsed a double.

You use an IllegalArgumentException when the parameter is of the right kind (compiles OK) but its value is illegal. eg parameter isDate dateOfBirth, the value passed is a date, but it's a date in the future. Or,the parameter is double price, the value passed is a number, but its <0.0

I think that's IllegalArgumentException if not please explain it.

please write a code that demonstrate IllegalArgumentException and arise exception at execution time

void setPrice(double newPrice) throws IllegalArgumentException  {
   if (price < 0.0) 
      throw new IllegalArgumentException("Price must be >= 0.0");

Now calling setPrice with a negative parameter will result in a IllegalArgumentException being thrown at run time.

i understand it but this is handled IllegalArgumentException i need a code that give unhandled IllegalArgumentException

I just showed how an IllegalArgumentException is created and thrown.

Because it's an unchecked exception you can handle it (via try/catch or by declaring your method as throws IllegalArgumentException) or you can ignore it and allow your program to crash when it is thrown.

handled/unhandled is not an attribute of the exception, it's an attribute of the code that can cause the execption to be thrown.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.