Checked / Unchecked Exception.

I was reading an article that discussed on Checked and Unchecked Exceptions. What I could understand is that checked exceptions are exceptions that are caught and thrown again, until a method decides to do something with the caught exception. An unchecked exception is one that is not caught at the time when the error happens, but later on.

Basically I know I understood in the wrong way, so I would be happy if someone here tries to explain it better. I searched on Google, and found C# does not have checked exceptions, so now I am questioning what they really are after all!

Thanks for any suggestions,

Regards,

Sim085

Ps: The following are the articles I have read:

http://www.artima.com/intv/handcuffs.html

http://www.theserverside.com/articles/article.tss?l=RodJohnsonInterview

[833 byte] By [sim085a] at [2007-10-2 10:55:54]
# 1
Checked exceptions are exceptions that you expect to occur and want the caller to handle explicitely. Unchecked exceptions are exceptions you do not expect to occur if nobody does anything weird like going past array boundaries.
CeciNEstPasUnProgrammeura at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 2

A checked exception happens at runtime. It is "thrown", and somewhere up the chain of scope it is "caught", sometimes within the same method, else skipping up until something does catch it and handle it.

An unchecked exception happens at compile time, specifically with regards to Generics checks done by the compiler which have detected that someone is violating or completely failing to use a Generics definition.

Laszlo.a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 3
Aw, ****. What's-his-face is correct. It's 3 hours past my bedtime.
Laszlo.a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 4
They block the word ****?
Laszlo.a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 5
What if I said I had piles of **** all over my desk?public transient crap;c.rap
Laszlo.a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 6

So if I do this ... is it an uncheked exception:

[code]

public void callMyMethod(){

try{

this.myMethod(UserInputHere);

}

cathc(IllegalArgumentException ile){

// Write error message here.

}

}

public void myMethod(int number){

if(number > 10){

throw new IllegalArgumentException("Error);

}

// Other Logic.

}

[code]Or ... Note I did the input as a user input, that is the user can sometimes enter 9 and sometimes 100.

regards,

sim085

sim085a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 7

Sorry for the above .. write it again.

So if I do this ... is it an uncheked exception:

public void callMyMethod(){

try{

this.myMethod(UserInputHere);

}

cathc(IllegalArgumentException ile){

// Write error message here.

}

}

public void myMethod(int number){

if(number > 10){

throw new IllegalArgumentException("Error);

}

// Other Logic.

}

Or? ... Note I did the input as a user input, that is the user can sometimes enter 9 and sometimes 100.

regards,

sim085

sim085a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 8

Checked exceptions :

- represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)

- are subclasses of Exception

- methods are obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)

Unchecked exceptions :

- represent defects in the program (often invalid arguments passed to a non-private method)

- are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException

- methods are not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

TimTheEnchantora at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 9
I'm about to be just as unhelpful as I was before (sorry). I just read several different web pages, all of which gave different and conflicting descriptions of checked, unchecked, caught, and uncaught exceptions.It's no wonder people are confused. Bedtime for Bonzo!
Laszlo.a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 10
But if checked exceptions are what you say, howcome the creator of C# said that they are not in C#, I am pretty sure that in C# exceptions are handled by the programmer as in Java!
sim085a at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 11
> Unchecked exceptions : > - represent defects in the program (often invalid> arguments passed to a non-private method) Or problems in the VM.> - are subclasses of RuntimeException, Or of Error.
jverda at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 12

> But if checked exceptions are what you say, howcome

> the creator of C# said that they are not in C#, I am

> pretty sure that in C# exceptions are handled by the

> programmer as in Java!

Mabe you could ask that on a C# forum.

The terms "checked" and "unchecked" are Java-specific I think. C# may use those terms differently or not at all.

jverda at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 13

> But if checked exceptions are what you say, howcome

> the creator of C# said that they are not in C#, I am

> pretty sure that in C# exceptions are handled by the

> programmer as in Java!

It's probably because exceptions are handled in the same way as in C++ (I don't know if that is true or not). A C++ method doesn't need to declare that methods are thrown, so all exceptions are unchecked.

Kaj

kajbja at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 14

> > But if checked exceptions are what you say,

> howcome

> > the creator of C# said that they are not in C#, I

> am

> > pretty sure that in C# exceptions are handled by

> the

> > programmer as in Java!

>

> It's probably because exceptions are handled in the

> same way as in C++ (I don't know if that is true or

> not). A C++ method doesn't need to declare that

> methods are thrown, so all exceptions are unchecked.

>

> Kaj

Maybe that's where the confusion is coming from.

Sim, I don't know about C#, but in C++, as Kaj says, you don't need to declare any exceptions--your method can just throw anything, but, if you do declare some, then (I think) the caller has to handle them.

And in Java, the caller can handle unchecked exceptions, but typically doens't.

jverda at 2007-7-13 3:20:29 > top of Java-index,Java Essentials,Java Programming...
# 15

> > But if checked exceptions are what you say,

> howcome

> > the creator of C# said that they are not in C#, I

> am

> > pretty sure that in C# exceptions are handled by

> the

> > programmer as in Java!

>

> Mabe you could ask that on a C# forum.

>

> The terms "checked" and "unchecked" are Java-specific

> I think. C# may use those terms differently or not at

> all.

C# doesn't have checked exceptions. All exceptions are like java unchecked exceptions. That is, the compiler does not check whether the exception is caught or thrown by the method.

pkwoostera at 2007-7-20 20:57:09 > top of Java-index,Java Essentials,Java Programming...
# 16

> Ps: The following are the articles I have read:

> http://www.artima.com/intv/handcuffs.html

You should also read the interview on that site with James Gosling: http://www.artima.com/intv/solid.html where he answers some of Anders Hejlsberg's comments in the interview you read.

pkwoostera at 2007-7-20 20:57:09 > top of Java-index,Java Essentials,Java Programming...