Skip to main content

Posts

Showing posts with the label exception-handling

Try-catch exception handling practice for iPhone/Objective-C

Apologies if this question has already been answered somewhere else, but I could not find any decisive answer when searching on it: I'm wondering when try-catch blocks are to be used in objective-c iPhone applications. Apple's "Introduction to the Objective-C Programming Language" state that exceptions are resource intensive and that one should "not use exceptions for general flow-control, or simply to signify errors." From reading a few related questions here I also gather that people are not often using this method in practice. So I guess the question is: what are the situations when it's appropriate to use try-catch blocks when developing for iPhone/Objective-C and when should they absolutely NOT be used? For example, I could use them to catch beyond bounds and other exceptions when working with objects in arrays. I have a method which performs are few tasks with objects that are passed on in a number of arrays. The method returns nil if an er

Why this null pointer exception when using BufferStrategy with swing timer in Java?

Why might I be getting the following exception with the below code? Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.awt.Component$BltBufferStrategy.showSubRegion(Unknown Source) at java.awt.Component$BltSubRegionBufferStrategy.show(Unknown Source) at javax.swing.BufferStrategyPaintManager.flushAccumulatedRegion(Unknown Source) ... It only happens about every other time I run it but always right at the start. I'm using if(bs.contentLost()){...} so I don't understand why it would be having problems. import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import java.util.Random; public class MLM2 extends JFrame implements ActionListener { private final Timer timer = new Timer(20, this); private Insets insets; private BufferStrategy bs; private BufferedImage drawing; int lastW; int lastH; int pos = 0; public static void main (String[] args) { MLM2 ex = new MLM2()

Filereader null declarations and appending best practice

I want to optimise my file reader function but am not sure if it is best practice to declare the nulls outside of the try loop. Also, is looping and appending chars to a Stringbuffer considered bad practice? I would like to use the exception handling here, but maybe it is better to use another structure? any advice most welcome, thanks. public String readFile(){ File f = null; FileReader fr = null; StringBuffer content = null; try{ f = new File("c:/test.txt"); fr = new FileReader(f); int c; while((c = fr.read()) != -1){ if(content == null){ content = new StringBuffer(); } content.append((char)c); } fr.close(); } catch (Exception e) { throw new RuntimeException("An error occured reading your file"); } return content.toString(); } }

Alternative solutions for android.os.NetworkOnMainThreadException

Now I'm solving the android.os.NetworkOnMainThreadException I put the connect method into separated thread, but when the thread starts, the start() method doesn't invoke the Run() , - and also using the AsyncTask , the task doesn't invoke the doInBackground() method! // For The AsyncTask my code was public class ConnectTask extends AsyncTask<URL, Integer, HttpEntity> { HttpEntity entity; String statue; LogInJSONActivity mainActivity; @Override protected HttpEntity doInBackground(URL... arg0) { // TODO Auto-generated method stub mainActivity.setString("Inside Do in background"); entity = connect(arg0[0]); mainActivity.setHttp(entity); return entity; } public HttpEntity connect(String url) { statue= "Inside Connect"; HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url);

iPhone - How to test allocated objects returned by each memory call?

When writing an app, you always have to write alloc/inits, get autoreleased datas returned by the framework classes, ... This may be 70% of the code, almost each single line of what you write... So... How do those returned object must be tested, to know if each of these calls have returned a correct object ? Testing the returned value each time, for each call, and treating the exception if you get nil where you expected an allocated object ? Letting the app crash ? How this must be done ?

Distinguish new vs existing exceptions

I am creating a PHP application and I want to display the number of times an error has occurred. The problem that I am trying to figure out is if an error has already been reported or if its new using the following values: Message (ie: Attempted to divide by zero.) Stack trace (ie: at componentNETapp.Form1.btnTrackExceptionsUn_Click(Object sender, EventArgs e) ...) Source (ie: componentNETapp) Target site (ie: Void btnTrackExceptionsUn_Click(System.Object, System.EventArgs)) Thanks