GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing stackify.com — Java Exception Handling: How to Specify and Handle Exceptions
Retrace will reach End of Life on March 31, 2027. Click here to learn more.
How to Specify and Handle Exceptions in Java
By: Thorben | July 17, 2017 Errors happen all the time in the software world. It might be an invalid user input or an external system that is not responding, or it’s a simple programming error. In all these situations, the errors occur at runtime and the application needs to handle them. Otherwise, it crashes and can’t process further requests. Java provides a powerful mechanism which allows you to handle the exceptional event where it occurred or in one of the higher methods in the call stack. In this article, we’ll cover the following topics:
Before we get into the details of Java’s exception handling, we need to define a few terms. Java Exception Handling: Common TerminologyCall StackThe call stack is the ordered list of methods that had been called to get to a specific method. In the context of this post, these are the methods which were called to get to the method in which the error occurred. Let’s have a look at an example. Method1 calls method2 which calls method3. The call stack now contains the following three entries:
Exception Class and HierarchyThe exception class identifies the kind of error that occurred. A NumberFormatException, for example, gets thrown when a String had the wrong format and couldn’t be converted into a number. As every Java class, the exception class is part of an inheritance hierarchy. It has to extend java.lang.Exception or one of its subclasses. The hierarchy is also used to group similar kinds of errors. An example for that is the IllegalArgumentException. It indicates that a provided method argument is invalid and it’s the superclass of the NumberFormatException. You can also implement your own exception classes by extending the Exception class or any of its subclasses. The following code snippet shows a simple example of a custom exception. public class MyBusinessException extends Exception {
private static final long serialVersionUID = 7718828512143293558L;
public MyBusinessException() {
super();
}
public MyBusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public MyBusinessException(String message, Throwable cause) {
super(message, cause);
}
public MyBusinessException(String message) {
super(message);
}
public MyBusinessException(Throwable cause) {
super(cause);
}
}
Exception ObjectAn exception object is an instance of an exception class. It gets created and handed to the Java runtime when an exceptional event occurred that disrupted the normal flow of the application. This is called “to throw an exception” because in Java you use the keyword “throw” to hand the exception to the runtime. When a method throws an exception object, the runtime searches the call stack for a piece of code that handles it. I will get into more details about exception handling in the How to Handle an Exception section of this post. Checked And Unchecked Exceptions in JavaJava supports checked and unchecked exceptions. You can use them in similar ways, and there are quite a few discussions about when to use which kind of exception. But that’s beyond the scope of this post. For now, let’s just follow the approach explained in Oracle’s Java Tutorial. You should use checked exceptions for all exceptional events that you can anticipate and that a well-written application should be able to handle. A checked exception extends the Exception class. A method that throws a checked exception or that calls a method that specifies a checked exception needs to either specify or handle it. Unchecked exceptions extend the RuntimeException. You should use them for internal errors that you can’t anticipate and that, most often, the application can’t recover from. Methods can but don’t need to handle or specify an unchecked exception. Typical examples that throw unchecked exceptions are:
How to Handle an ExceptionJava provides two different options to handle an exception. You can either use the try-catch-finally approach to handle all kinds of exceptions. Or you can use the try-with-resource approach which allows an easier cleanup process for resources. Try-Catch-FinallyThat is the classical approach to handle an exception in Java. It can consist of 3 steps:
The try block is required, and you can use it with or without a catch or finally block. The Try BlockLet’s talk about the try block first. It encloses the part of your code that might throw the exception. If your code throws more than one exception, you can choose if you want to:
The following example shows a try block which encloses three method calls. public void performBusinessOperation() {
try {
doSomething("A message");
doSomethingElse();
doEvenMore();
}
// see following examples for catch and finally blocks
}
public void doSomething(String input) throws MyBusinessException {
// do something useful ...
throw new MyBusinessException("A message that describes the error.");
}
public void doSomethingElse() {
// do something else ...
}
public void doEvenMore() throws NumberFormatException{
// do even more ...
}
As you can see in the method definitions, only the first and the third method specify an exception. The first one might throw a MyBusinessException, and the doEvenMore method might throw a NumberFormatException. In the next step, you can define one catch block for each exception class you want to handle and one finally block. All checked exceptions that are not handled by any of the catch blocks need to be specified. The Catch BlockYou can implement the handling for one or more exception types within a catch block. As you can see in the following code snippet, the catch clause gets the exception as a parameter. You can reference it within the catch block by the parameter name. public void performBusinessOperation() {
try {
doSomething("A message");
doSomethingElse();
doEvenMore();
} catch (MyBusinessException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
The previous code sample shows two catch blocks. One to handle the MyBusinessException and one to handle the NumberFormatException. Both blocks handle the exceptions in the same way. Since Java 7, you can do the same with just one catch block. public void performBusinessOperation() {
try {
doSomething("A message");
doSomethingElse();
doEvenMore();
} catch (MyBusinessException|NumberFormatException e) {
e.printStackTrace();
}
}
The implementation of the catch blocks in the previous examples is very basic. I just call the printStackTrace method which writes the class, message and call stack of the exception to system out. com.stackify.example.MyBusinessException: A message that describes the error. at com.stackify.example.TestExceptionHandling.doSomething(TestExceptionHandling.java:84) at com.stackify.example.TestExceptionHandling.performBusinessOperation(TestExceptionHandling.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) In a real application, you might want to use a more advanced implementation. You can, for example, show an error message to the user and request a different input or you could write a record into the work log of your batch process. Sometimes, it might even be ok to catch and ignore the exception. And in production, you also need to monitor your application and its exception handling. That’s where Retrace and its error monitoring capabilities become very helpful. The Finally BlockThe finally block gets executed after the successful execution of the try block or after one of the catch blocks handled an exception. It is, therefore, a good place to implement any cleanup logic, like closing a connection or an InputStream. You can see an example of such a cleanup operation in the following code snippet. The finally block will be executed, even if the instantiation of the FileInputStream throws a FileNotFoundException or the processing of the file content throws any other exception. FileInputStream inputStream = null;
try {
File file = new File("./tmp.txt");
inputStream = new FileInputStream(file);
// use the inputStream to read a file
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
As you’ve seen, the finally block provides a good option to prevent any leaks. And before Java 7, it was a best practice to put all cleanup code into a finally block. Try-With-ResourceThat changed when Java 7 introduced the try-with-resource statement. It automatically closes all resources that implement the AutoCloseable interface. And that is the case for most Java objects that you need to close. The only thing you need to do to use this feature is to instantiate the object within the try clause. You also need to handle or specify all exceptions that might be thrown while closing the resource. The following code snippet shows the previous example with a try-with-resource statement instead of a try-catch-finally statement. File file = new File("./tmp.txt");
try (FileInputStream inputStream = new FileInputStream(file);) {
// use the inputStream to read a file
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
As you can see, the try-with-resource statement is a lot easier to implement and read. And the handling of the IOException, which might be thrown while closing the FileInputStream, doesn’t require a nested try-catch statement. It is now handled by a catch block of the try-with-resource statement. How to Specify an ExceptionIf you don’t handle an exception within a method, it will be propagated within the call stack. And if it’s a checked exception, you also need to specify that the method might throw the exception. You can do that by adding a throws clause to the method declaration. As a result, all calling methods need to either handle or specify the exception themselves. If you want to indicate that a method might throw an unchecked exception, you may specify this as well. public void doSomething(String input) throws MyBusinessException {
// do something useful ...
// if it fails
throw new MyBusinessException("A message that describes the error.");
}
Handle or Specify an ExceptionAs so often, it depends on the use case if you should handle or specify an exception. And as you might guess, it’s difficult to provide a recommendation that’s a good fit for all use cases. In general, you need to ask yourself the following questions:
If you answer both questions with yes, you should handle the exception within your current method. In all other situations, it’s most likely better to specify it. That allows the caller of your class to implement the handling as it fits the current use case. SummaryOK, that’s all about Java exception handling for now. I will get into more details about best practices and common errors in future posts of this series. As you’ve seen, Java offers you two general types of exceptions: The checked and the unchecked exception. You should use a checked exception for all exceptional events that can be expected and handled by the application. You need to decide if you want to handle it within a method or if you specify it. You can handle it with a try-catch-finally or a try-with-resource block. If you decide to specify the exception, it becomes part of the method definition, and the exception needs to be specified or handled by all calling methods. You should use an unchecked exception for internal errors that can’t be anticipated. You are not required to handle or specify this kind of exception, but you can do it in the same way as you handle or specify a checked exception. When using Retrace APM with code profiling, you can collect exceptions directly from Java, without any code changes! To write better code on your workstation, try Prefix, Stackify’s free code profiler. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python. Related posts:Improve Your Code with Retrace APMStackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world. Get the latest news, tips, and guides on software development.Join the community of 40,000 developers that subscribe to our newsletter. Popular Posts
indobet
toto slot
toto
toto
bobatoto daftar
logototo
patentoto
situs toto slot 4D
toto
bayitoto
popotogel prize hongkong malam ini
joker11
lingkartoto
depo 5k
slot
sbobet88
sbobet88
https://nolimithoki.dev/
dinasti33 slot
slot jepang
https://www.juara288.asia/
slot 4d
toto
toto
situs toto
flokitoto
slot gacor
logototo
slot depo 5k
bobatoto
bobatoto
bobatoto
sontogel
mpo slot
situs toto
slot
situs slot
bobatoto
kari4d
kari4d
kari4d
kari4d
pascol4d
pascol4d
pascol4d
slot gacor
bento11 link
slot gacor
toto
toto slot
toto
situs slot
situs slot
situs slot
https://www.asupan188.dev/
slot gacor gampang menang
toto
slot gacor
bwo303
situs toto
toto slot
slot gacor
slot88
slot gacor
situs slot
link slot
slot gacor
pucuk4d
PASCOL4D
situs slot
link slot
pajaktoto
slotvip
situs toto
toto slot
situs slot
situs slot
situs slot
toto
slot gacor
slot777
situs slot
link slot
situs judi bola
POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat
link slot
slot gacor
situs slot
slot gacor
slot gacor
situs slot
link slot
gacor
Slot88
Slot88
slot gacor
situs toto
toto
slot pulsa
slot777
link slot gacor
situs gacor
slot gacor
leon188
slot gacor
slot gacor
slot gacor
Slot88
slot gacor
situs slot
slot gacor
Slot88
slot gacor
slot terbaru
slot gacor
situs gacor
situs slot
slot gacor
situs slot
Situs Slot
POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya
StreamEast
Jpmania
slot resmi
slot
slot resmi
slot
bayitoto
patentoto
link gacor
slot gacor
kari4d
slot gacor
AMANAHTOTO
slot gacor
slot gacor
AMANAHTOTO
pascol4d
kari4d
AMANAHTOTO
kari4d
bragatoto
dokter88
slot gacor
slot game
patentoto
pucuk4d
rokokbet
rokokbet
kari4d
pascol4d
LOGIN PASCOL4D
pucuk4d
roma77
judi bola
slot depo 10k
mpo slot
Girang4d
KARI4D
StreamEast
AMANAHTOTO
jutatoto
PARLAY
slot88
slot depo 5k
hoki99
pascol4d
pkv games
roma77
Slot
poker
slot
indobet
toto slot
toto
toto
bobatoto daftar
logototo
patentoto
situs toto slot 4D
toto
bayitoto
popotogel prize hongkong malam ini
joker11
lingkartoto
depo 5k
slot
sbobet88
sbobet88
https://nolimithoki.dev/
dinasti33 slot
slot jepang
https://www.juara288.asia/
slot 4d
toto
toto
situs toto
flokitoto
slot gacor
logototo
slot depo 5k
bobatoto
bobatoto
bobatoto
sontogel
mpo slot
situs toto
slot
situs slot
bobatoto
kari4d
kari4d
kari4d
kari4d
pascol4d
pascol4d
pascol4d
slot gacor
bento11 link
slot gacor
toto
toto slot
toto
situs slot
situs slot
situs slot
https://www.asupan188.dev/
slot gacor gampang menang
toto
slot gacor
bwo303
situs toto
toto slot
slot gacor
slot88
slot gacor
situs slot
link slot
slot gacor
pucuk4d
PASCOL4D
situs slot
link slot
pajaktoto
slotvip
situs toto
toto slot
situs slot
situs slot
situs slot
toto
slot gacor
slot777
situs slot
link slot
situs judi bola
POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat
link slot
slot gacor
situs slot
slot gacor
slot gacor
situs slot
link slot
gacor
Slot88
Slot88
slot gacor
situs toto
toto
slot pulsa
slot777
link slot gacor
situs gacor
slot gacor
leon188
slot gacor
slot gacor
slot gacor
Slot88
slot gacor
situs slot
slot gacor
Slot88
slot gacor
slot terbaru
slot gacor
situs gacor
situs slot
slot gacor
situs slot
Situs Slot
POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya
StreamEast
Jpmania
slot resmi
slot
slot resmi
slot
bayitoto
patentoto
link gacor
slot gacor
kari4d
slot gacor
AMANAHTOTO
slot gacor
slot gacor
AMANAHTOTO
pascol4d
kari4d
AMANAHTOTO
kari4d
bragatoto
dokter88
slot gacor
slot game
patentoto
pucuk4d
rokokbet
rokokbet
kari4d
pascol4d
LOGIN PASCOL4D
pucuk4d
roma77
judi bola
slot depo 10k
mpo slot
Girang4d
KARI4D
StreamEast
AMANAHTOTO
jutatoto
PARLAY
slot88
slot depo 5k
hoki99
pascol4d
pkv games
roma77
Slot
poker
slot
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |