-
Notifications
You must be signed in to change notification settings - Fork 3
Exceptions
###About Sometimes i go to the inteview and always have questions about Exceptions and its handling. That's why i want to gather all my minds and thoughts about this theme.
And let's start.
We always hear about Exceptions, but what Exception is?
In simple words - Exception is a signal about non-standard situations. But itsn't extraordinary or outstanding things.
And it's different situations - we can't close file, delete something or another.
As we can see below, we have super class - java.lang.Throwable
and two successors - java.lang.Exception
and java.lang.Error
And java.lang.Exception
is a super class for java.lang.RuntimeException
java.lang.Throwable
and java.lang.Exception
is checked exception, java.lang.RuntimeException
and java.lang.Error
is unchecked.
We can catch exceptions. And we can catch all kinds of Exceptions.
But first of all, let's answer the question - what's the difference between Error and Exception?
As for me, i think that Error - is more serious situation than Exception. If we can't create objects because we havn't memory, we call nonexistent method - it's Error
.
But if we can't close file, or can't convert String to Interger - it's a situation after which we can continue our application, we can handle it(for example - ask the user to enter the number again) - it's java.lang.Exception
.
As you can see we have - logical separation.
We have two Exceptions - java.lang.Exception
and java.lang.RuntimeException
. RuntimeException - it's Exception which we have in Runtime, when we work with our application.
Exceprions - is a class, and that's why we can put to the our exceptions parameters and methods, our logic, if we want. We should remember that it's expensive - create a lot of exceptions, we should throw it only when we really need.
-
java.lang.Exception For example, when we have wrong data - we can't do anything except say:
Man! Something wrong!
- And that's why in situations like this we have java.lang.Exception. We should check this exception - it's necessarily. If we don't handle this type of exceptions - we can't compile our program. -
java.lang.RuntimeException It's developer error. When we devide on zero or have null pointer - it's our error, and we have exception in runtime. We can skip handling this type of exceptions because we do not expect an error. But we can catch it.
-
java.lang.Error Ir's critical errors after which we can't continue our job, for example, we havn't enough memory. But we can catch it, if we know what we want to do with this situation.
We can throw any exception which we want.
We can catch exceptions by try/catch/finally.
And we should think how to arrange the catch blocks. If we have method which can throw IOException and Exception, and we write smth like this:
try {
method();
} catch (Exception e) {//do some logic 1}
catch (IOException e) {//do some logic 2}
We can't reach the IOException handling, because we have more common catch block above. Think about it.
In some cases, when we want to catch all exceptions from method - we can write:
try {
method();
} catch (Throwable t) {//do some logic 1}
But we catch RE and Errors too!
For example try to catch RE:
try {
String numberAsString = "one";
Double res = Double.valueOf(numberAsString);
} catch (RuntimeException re) {
System.out.println("Error while convert string to double!");
}
And let's catch Error:
try {
throw new Error();
} catch (RuntimeException re) {
System.out.println("RE");
} catch (Error error) {
System.out.println("ERROR");
}
}
It's helpful to have your own Exceptions. For example, we have method2() and this method can throw 3 different Exception. If we have our hierarchy - we simply write 3 catch block for each exception. if we havn't this - we have one catch block with Exception signature where we catch our exceptions and try to understand wtf.
If our exception interrupt thread we can use Thread.UncaughtExceptionHandler
.
And be carefull with handling!
public static void main(String[] args){
try {
try {
throw new Exception("0");
} finally {
if (true) {
throw new IOException("1");
}
System.err.println("2");
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
} catch (Exception ex) {
System.err.println("3");
System.err.println(ex.getMessage());
}
}
After this code we have "1" on output!
####To sum up
- Use checked exceptions if we know that in this case we can have error.
- Use unchecked exceptions if error - is our mistake.
- Try to have your own exceptions with messages.
- Always use finally, if you work with resources and try to use try-with-resources
- We can throw our exception by
throw
- We can show that the method can throw an exception by
throws