- #1
Chromium
- 56
- 0
Hey Everyone,
I have a question regarding exception handling. First I'll explain what I know (or at the very least, what I believe I know) about exceptions just to be sure I have an idea of what's going on, then present the question.
To let any calling methods know that a certain method might throw a certain exception, you use a throws clause in the method signature. For example:
public void doSomething( ) throws someException
{
Also, note that in the method body, a new instance of someException is created, and then thrown to the calling routine. In this particular case, the doSomething method does not care about handling the exception, it just passes the exception to what code called it.
Now, you could also handle exceptions in the same method in which the exception occurs by using what is known as a try block. For example
public void doSomething( )
{
The code in the try block may or may not throw a new someException. However, in case it does, there is a catch statement that could handle the exception. In the event an exception does occur, the exception is caught, and control is returned to the code immediately after the last catch statement.
Finally, my question is: Why is it that in some try blocks and method signatures with a throws clause there is no code to create and throw a new exception object? How can code like the following be legal?
public void myMethod() throws myException
{
}
or
try
{
If someone could explain this to me, or refer me to a site explaining why this is acceptable, it would be greatly appreciated. Thanks
I have a question regarding exception handling. First I'll explain what I know (or at the very least, what I believe I know) about exceptions just to be sure I have an idea of what's going on, then present the question.
To let any calling methods know that a certain method might throw a certain exception, you use a throws clause in the method signature. For example:
public void doSomething( ) throws someException
{
code...;
code...;
throw new someException;
}code...;
throw new someException;
Also, note that in the method body, a new instance of someException is created, and then thrown to the calling routine. In this particular case, the doSomething method does not care about handling the exception, it just passes the exception to what code called it.
Now, you could also handle exceptions in the same method in which the exception occurs by using what is known as a try block. For example
public void doSomething( )
{
code...;
code...;
try
{
catch (myException)
{
catch (someException)
{
code...;
try
{
code...;
throw new someException;
}throw new someException;
catch (myException)
{
code...;
code...;
}code...;
catch (someException)
{
code...;
code...;
code...;
}
}The code in the try block may or may not throw a new someException. However, in case it does, there is a catch statement that could handle the exception. In the event an exception does occur, the exception is caught, and control is returned to the code immediately after the last catch statement.
Finally, my question is: Why is it that in some try blocks and method signatures with a throws clause there is no code to create and throw a new exception object? How can code like the following be legal?
public void myMethod() throws myException
{
}
or
try
{
code...;
}If someone could explain this to me, or refer me to a site explaining why this is acceptable, it would be greatly appreciated. Thanks