131-140 Flashcards
What is the difference between error and exception?
An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. The cause the application to come to a halt.
Exceptions are conditions that occur because of a bad input or human error. E.g. FileNotFoundException. In most cases it is possible to recover from an exception
How can you handle Java exceptions?
Keywords are:
try
catch
finally
throw
throws
What are the differences between a checked and unchecked exception?
Checked exception:
- The classes that extend Throwable class except for RuntimeException
- They are checked at compile time
Unchecked Exception:
- The classes that extend RuntimeException
- They are not checked at compile time
How does an exception propagate in the code?
If an exception is not caught, it is thrown from the top of the stack and falls down the call stack to the previous procedure, and so on until it is caught or the call stack reaches the bottom. This is known as Exception propagation
How do you create a custom exception?
To create a new exception you extend the exception class or any of its subclasses.
E.g.
checked Exception -> class New1Exception extends Exception {}
unchecked Exception -> class New2Exception extends NullPointerException {}
Can we write multiple catch blocks under a single try clock?
Yes we can, but the approach should be from specific to general
What are the differences between processes and threads?
Process - An executing instance of a program
Thread - A subset of the process
Process - must use inter-process communication to communicate with sibling processes
Thread - can communicate with any thread within its process
Process - can only exercise control over child processes
Thread - can exercise considerable control over other threads
Process - Run in separate memory spaces
Thread - Run in shared memory space
Process - Are controlled by the operating system
Thread - Are controlled by a programmer in a program