aspose file tools
The moose likes Java in General and the fly likes Changing unchecked Exceptions to Checked Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Changing unchecked Exceptions to Checked" Watch "Changing unchecked Exceptions to Checked" New topic
Author

Changing unchecked Exceptions to Checked

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32600
    
    4
Every now and again I think it would be useful to throw a checked Exception rather than returning a null value.
So I think which Exception to use, and NullPointerException comes to mind. So I write "throws NullPointerException," and "throw new NullPointerException();" and let the compiler at it . . . and nothing happens.
Because NullPointer is an unchecked Exception. Not like IOException, where I get told off if I don't cover the code liberally with try-catches.

Is there any way of changing it so it behaves as a checked Exception?

I have tried all sorts of things, looking through the API and going through the source code for Exception and NullPointerException, and not found any way to do it. Is it possible at all?
Or ought I to look for a different Exception?

CR
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

A few exceptions in the API exist in both checked and unchecked forms: NoClassDefFoundError and ClassNotFoundException, for example, are basically the same thing, used in different contexts. But there's no general solution.

There's no Exception in the standard API that represents a general "can't compute a result, so have to return null" that comes to mind; I'd simply define my own. In my code, I find that unless I'm implementing someone else's interface, I rarely throw an exception from the API, preferring to use custom ones.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32600
    
    4
Thank you.

It seems I was right as far as I had got.

Campbell
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Changing unchecked Exceptions to Checked
 
Similar Threads
Exceptions
doubt in exception
Checked and Unchecked exception
Explanation fro Exceptions
parsing question