Diagnostic Java code: empty flag Error mode

Source: Internet
Author: User

Empty flag Error Mode

In my last article, I explained how the use of NULL pointers in place of different basic types of data is one of the most common causes of nullpointerexception anomalies. This time, I will explain how an empty pointer instead of an exception can cause problems. In Java programs, exception situations are typically handled by throwing exceptions and capturing them at the appropriate control point. But the often-seen approach is to indicate this by returning a null pointer value (and, possibly, printing a message to System.err). If the calling method does not explicitly check the null pointer, it may attempt to discard the return value and trigger a null-pointer exception.

You might guess that this pattern is called an empty flag error pattern because it is inconsistent with a null pointer as a sign of an exception condition.

Cause

Let's consider the following simple bridge class (from Bufferedreaders to iterators):

import java.io.BufferedReader;
import java.io.InputStreamReader;
Import java.io.IOException;
Import Java.util.Iterator;  
public class Bufferedreaderiterator implements iterator {
Private bufferedreader internal;
  Public Bufferedreaderiterator (BufferedReader _internal) {
this.internal = _internal;
   
Public boolean Hasnext () {
try {

Boolean result = true;
Let's suppose that lines in the underlying input stream are known
//To be no greater than-characters br> Internal.mark (80);    
if (this.next () = = null) {
result = false;
}
Internal.reset ();
return result;    
}
catch (IOException e) {
System.err.println (e.tostring ());
return false;   
}
}
Public Object next () {
try {
return internal.readline ();
   
catch (IOException e) {
System.err.println (e.tostring ());
Return null;   
}
}
public void Remove () {
//This iterator does not support the remove operation.
throw new Unsupportedoperationexception ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.