Results 1 to 2 of 2
  1. #1
    himaece is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default unreported exception java.io.ioexception must be caught or declared to be thrown

    Hi all,

    I'm new to java n forum.....
    I am developing java application that can read/write to comm port.
    While i was reading i don't have any problem
    but while write with timer i got this error .....
    any one please help me....

    Here is my code..


    Java Code:
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class SimpleRead implements Runnable, SerialPortEventListener {
        static CommPortIdentifier portId;
        static Enumeration portList;
    	String messageString = "AT+CMGL=" + '"' + "REC READ" + '"' + '\r' + '\n';
        InputStream inputStream;
    	//public OutputStream outputStream;
    	OutputStream outputStream;
        public SerialPort serialPort;
        Thread readThread;
    	private int times = 0;
    	//Thread writeThread;
        public static void main(String[] args) {
    		portList = CommPortIdentifier.getPortIdentifiers();
    		Timer timer = new Timer("Printer");
    		MyTask t = new MyTask();
    		timer.schedule(t, 0, 2000);
            while (portList.hasMoreElements()) {
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                     if (portId.getName().equals("COM9")) {
    			//   if (portId.getName().equals("/dev/term/a")) {
                        SimpleRead reader = new SimpleRead();
                    }
                }
            }
        }
    
        public SimpleRead() {
            try {
                serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
            } catch (PortInUseException e) {System.out.println(e);}
            try {
                inputStream = serialPort.getInputStream();
    			outputStream = serialPort.getOutputStream();
            } catch (IOException e) {System.out.println(e);}
    	try {
                serialPort.addEventListener(this);
    	} catch (TooManyListenersException e) {System.out.println(e);}
            serialPort.notifyOnDataAvailable(true);
            try {
                serialPort.setSerialPortParams(19200,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e) {System.out.println(e);}
            readThread = new Thread(this);
            readThread.start();
        }
    
        public void run() {
            try {
    			/*//private int times = 0;
    			times++;
    			if (times <= 15) {
                outputStream.write(messageString.getBytes());
    			} else {
                System.out.println("Timer stops now...");
     
                //Stop Timer.
                t.cancel();
    			}*/
                Thread.sleep(20000);
    			} catch (InterruptedException e) {System.out.println(e);}
        }
    
        public void serialEvent(SerialPortEvent event) {
            switch(event.getEventType()) {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            //case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            case SerialPortEvent.DATA_AVAILABLE:
                byte[] readBuffer = new byte[200];
    			//String Str2 = "AT+CMGL=" + '"' + "REC UNREAD" + '"' ;
                try {
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(readBuffer);
                    }
                    System.out.print(new String(readBuffer));
    				/*if (outputStream.available() > 0)
    				{
    					outputStream.write(messageString.getBytes());
    				}*/
    				/*try
    				{
    					outputStream.write(messageString.getBytes());
    				}
    				catch (IOException e)
    				{
    					System.out.println(e);
    				}*/
    				//outputStream.writeBytes(Str2 + '\r' + '\n');
                } catch (IOException e) {System.out.println(e);}
    			//try
    			//{
    			//	outputStream.write(messageString.getBytes());
    			//}
    			//catch (NullPointerException e)
    			//{
    			//	System.out.println(e);
    			//}
                break;
            }
        }
    	
    }
    class MyTask extends TimerTask {
    	//OutputStream outputStream;
    	//String messageString = "AT+CMGL=" + '"' + "REC READ" + '"' + '\r' + '\n';
        //times member represent calling times.
        private int times = 0;
    	//outputStream = serialPort.getOutputStream();
     
        public void run() {
            times++;
            if (times <= 15) {
    			/*try
    			{
    				outputStream.write(messageString.getBytes());
    			}
    			catch (IOException e)
    			{
    				System.out.println(e);
    			}*/
                
    			outputStream.write(messageString.getBytes());
    			System.out.println("Timer is alive...");
            } else {
                System.out.println("Timer stops now...");
     
                //Stop Timer.
                this.cancel();
            }
        }
    }
    Last edited by quad64bit; 11-09-2012 at 05:11 PM. Reason: readability

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: unreported exception java.io.ioexception must be caught or declared to be thrown

    Moved from Advanced Java.

    Please post your code in [code][/code] tags so that it retains its formatting.

    Go through the Exceptions and Basic I/O parts of this tutorial: Trail: Essential Classes (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2012, 02:43 PM
  2. Replies: 7
    Last Post: 03-21-2012, 08:07 AM
  3. unreported exception java.io.IOException
    By fluffaykitties in forum New To Java
    Replies: 11
    Last Post: 03-07-2011, 01:59 AM
  4. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  5. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 04:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •