Adding to the info on the throws
keyword for Java, it appears you may have written a program that does in fact not execute. Or rather, it is halted by an unhandled exception. You say
But if i add public static void myMethod() throws myArrayException
and remove the if condition
If by "the if condition", you mean this check:
if (size < 0) {
throw new myArrayException();
}
Then MyMethod
no longer throws myArrayException
. Instead, the statement int[] a = new int[size];
will throw a NegativeArraySizeException
, which you are not catching, making it an unhandled exception, which usually halts execution of the program. In that case, the line System.out.print("code successfully reached");
also won't execute, making it appear your program is doing nothing.
To answer your specific question (full disclosure, it's been forever since I used Java), the throws
keyword is more a disclaimer on your method. It doesn't affect the compilation or execution of the method. As baraky excellently states:
throws
means that the function can throw the exception. It doesn't mean that it always throws the exception.
- "myMethod throws myArrayException " have any effect on my code? Nope, no effect. It's just a warning to others.
- If i remove the whole "if" block then JRE itself handles it? JRE handles any uncaught exception because it is the highest level program. If you remove the if block and the
throw
statement inside of it, then a NegativeArraySizeException
is thrown when a netagive size is passed in (from the line of the array allocation: int[] a = new int[size];
), instead of a MyArrayExcpetion
.
- So the
throw
statement in myMethod is important and not myMethod throws myArrayException
? Important is a relative term. The throw
statement is important to the immediately executing code as it has an impact on the program flow and execution. The throws myArrayException
on the method declaration does not have any immediate impact. BUT if you expect this code to be widely used, by others or even by yourself over a period of time long enough where you might not remember what you were thinking when you wrote every last line of the application, then it becomes very important, because...
- because, even without this statement code works fine.... why add this statement?
throws
basically tells whoever else looks at your code "Hey, you better handle these exceptions if you call this code or else your program might crash." You don't have to include this declaration, but it makes everyone's lives easier if you do.
If your method catches an exception, then it should not say it throws that exception. Breaking it down in code using the "checked vs. unchecked" terminology that seems to have become prevalent since I used Java:
public static void MyMethod() throws MyArrayException{
int size = 5;
if (size < 0) {
// Unchecked exception, properly warned callers of MyMethod that you will
// throw it and they need to catch it
throw new MyArrayException();
}
if (size == 3) {
// Unchecked exception, but you didn't warn callers of MyMethod
// No functional impact for your code, but if someone else is relying
// on this method, they won't know catch this and will likely be
// irate with you if it crashed their program
throw new IHateThreesException();
}
try {
int[] a = new int[size];
}
catch(NegativeArraySizeException)
{
// This is a checked exception, because you checked for it with a
// try-catch block and don't re-throw it
System.out.print("Code checked for this but size " + size + " was negative anyways!");
}
catch(OutOfMemoryExcpetion ome)
{
// I _think_ this is still an unchecked exception, because the I choose
// to re-throw it. Regardless, we explicitly throw this exception so
// there is no excuse for us to not have included it in the throws
// declaration
System.out.print("I just canno' do it captain, I don't have the memory!"
+ "(Free up some memory and try again)");
throw ome;
}
System.out.print("code successfully reached");
}
The examples are cobbled together from what others have said, and my own feeble memory of Java. I don't know the exact definition of a checked exception, but hopefully I capture at least the essence of what it is and how it differs from an unchecked exception. Please correct me if I got this wrong.
Edit: Doing some more reading, I think I am very wrong about what is and is not a checked exception. Hopefully someone can correct me.