2

Are there any keywords or special syntax of code that if I look at it, I'll know that here unchecked exception is being used or a checked exception is being used?

From reading previous posts that asked this question before, it seems to me that the only way to know if it's a check or unchecked is to see if the exception is extending Exception or RuntimeException.

Is that really the only way to know? Because I thought the syntax for writting unchecked and checked exceptions were supposed to be different too, but I'm not sure what to look for.

Viola
  • 33
  • 1
  • 1
  • 8
  • A *sign* that you're using checked exceptions is that you have `try/catch` blocks or the method has `throws` in the signature. This isn't foolproof, because you can catch/throws unchecked exceptions too; and you can "over-catch"/"over-throws" too broad an exception (as in, you can catch/throws Exception when actually only RuntimeException will be thrown); but since you *have to* do one of these for checked exceptions, this is a sign that they are likely being used. – Andy Turner Oct 25 '17 at 02:35
  • My question would be *why do you want to know*? What difference will it make to you, knowing this information? Is it simply to learn the difference, or is there a more practical purpose? – Andy Turner Oct 25 '17 at 02:36
  • See if it extends RuntimeException or Error. Those are the only things that are unchecked. No keywords or different syntax. – Nathan Hughes Oct 25 '17 at 02:36
  • Honestly the reason I want to known is because my homework asks to justify why I used a checked or unchecked exception in my code. But, after i finished writing the code (and it works) I couldn't tell whether what I did was checked or unchecked cause I still don't really understand the difference. – Viola Oct 25 '17 at 02:46
  • Show your code. – Andy Turner Oct 25 '17 at 02:47
  • No problem. The compiler will tell you. If it's a checked exception and you don't have an appropriate `throws` declaration or `catch` block, you will get a compilation error. If you want to know whether you're using checked exceptions, remove all `throws` and `catch` blocks and see whether it compiles. – user207421 Oct 25 '17 at 03:49

2 Answers2

1

From Java document. it says "The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions." So your answer is Yes. But remember Error is also one type of unchecked exceptions.

Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.From You can use "isInstance()" function in Java to see if a specific exception belongs to a certain class. In your case, it would be if an exception is a RuntimeException. If yes, it is unchecked, if no, checked.

You can also look for "java.lang.Object.getClass()" which can tell you the exact runtime class of the object.

The other way I can think about handle them separately. You can catch RuntimeException and Error, then catch all other exceptions which are not the instances of RuntimeException.

Peipei
  • 136
  • 1
  • 9
-1

In practice, a checked Exception is used when you expect there will be an exception in your program, and you want the caller to handle it.

e.g.

When you read in a file from FileSystem, there would be a chance that the file is not exist, so IOException will be thrown and the caller has to handle it by try..catch. Such an exception is only an exceptional flow of your program and it is not a bug at all.

An unchecked Exception is used when you do not expect it is unlikely to happen at all.

e.g.

When you compare 2 strings, a.equals(b), you do not expect a is null, so you won't check if NullPointerException will be thrown. If it really throws an unchecked exception, then there is a bug in your program.

Alex
  • 803
  • 4
  • 9