Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code? -
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.io.*; public class encrypturl extends japplet implements actionlistener { container content; jtextfield username = new jtextfield(); jtextfield firstname = new jtextfield(); jtextfield lastname = new jtextfield(); jtextfield email = new jtextfield(); jtextfield phone = new jtextfield(); jtextfield heartbeatid = new jtextfield(); jtextfield regioncode = new jtextfield(); jtextfield retregioncode = new jtextfield(); jtextfield encryptedtextfield = new jtextfield(); jpanel finishpanel = new jpanel(); public void init() { //settitle("book - e project"); setsize(800,600); content = getcontentpane(); content.setbackground(color.yellow); content.setlayout(new boxlayout(content, boxlayout.y_axis)); jbutton submit = new jbutton("submit"); content.add(new jlabel("user name")); content.add(username); content.add(new jlabel("first name")); content.add(firstname); content.add(new jlabel("last name")); content.add(lastname); content.add(new jlabel("email")); content.add(email); content.add(new jlabel("phone")); content.add(phone); content.add(new jlabel("heartbeatid")); content.add(heartbeatid); content.add(new jlabel("region code")); content.add(regioncode); content.add(new jlabel("retregioncode")); content.add(retregioncode); content.add(submit); submit.addactionlistener(this); } public void actionperformed(actionevent e) { if(e.getactioncommand() == "submit"){ string subusername = username.gettext(); string subfname = firstname.gettext(); string sublname = lastname.gettext(); string subemail = email.gettext(); string subphone = phone.gettext(); string subheartbeatid = heartbeatid.gettext(); string subregioncode = regioncode.gettext(); string subretregioncode = retregioncode.gettext(); string concaturl = "user="+ subusername + "&f="+ subfname + "&l=" +sublname+ "&em=" + subemail + "&p="+subphone+"&h="+subheartbeatid+"&re="+subregioncode+ "&ret=" + subretregioncode; concaturl = padstring(concaturl, ' ', 16); byte[] encrypted = encrypt(concaturl); string encryptedstring = bytestohex(encrypted); content.removeall(); content.add(new jlabel("concatenated user input -->" + concaturl)); content.add(encryptedtextfield); setcontentpane(content); } } public static byte[] encrypt(string toencrypt) throws exception{ try{ string plaintext = toencrypt; string key = "01234567890abcde"; string iv = "fedcba9876543210"; secretkeyspec keyspec = new secretkeyspec(key.getbytes(), "aes"); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher cipher = cipher.getinstance("aes/cbc/nopadding"); cipher.init(cipher.encrypt_mode,keyspec,ivspec); byte[] encrypted = cipher.dofinal(toencrypt.getbytes()); return encrypted; } catch(exception e){ } } public static byte[] decrypt(byte[] todecrypt) throws exception{ string key = "01234567890abcde"; string iv = "fedcba9876543210"; secretkeyspec keyspec = new secretkeyspec(key.getbytes(), "aes"); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher cipher = cipher.getinstance("aes/cbc/nopadding"); cipher.init(cipher.decrypt_mode,keyspec,ivspec); byte[] decrypted = cipher.dofinal(todecrypt); return decrypted; } public static string bytestohex(byte[] data) { if (data==null) { return null; } else { int len = data.length; string str = ""; (int i=0; i<len; i++) { if ((data[i]&0xff)<16) str = str + "0" + java.lang.integer.tohexstring(data[i]&0xff); else str = str + java.lang.integer.tohexstring(data[i]&0xff); } return str; } } public static string padstring(string source, char paddingchar, int size) { int padlength = size-source.length()%size; (int = 0; < padlength; i++) { source += paddingchar; } return source; } }
i'm getting unreported exception
java.lang.exception; must caught or declared thrown byte[] encrypted = encrypt(concaturl);
as as
.java:109: missing return statement
can me solving these problems?
all problems derive this
byte[] encrypted = cipher.dofinal(toencrypt.getbytes()); return encrypted;
which enclosed in try, catch block, problem in case program found exception not returning anything. put (modify program logic stands):
public static byte[] encrypt(string toencrypt) throws exception{ try{ string plaintext = toencrypt; string key = "01234567890abcde"; string iv = "fedcba9876543210"; secretkeyspec keyspec = new secretkeyspec(key.getbytes(), "aes"); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher cipher = cipher.getinstance("aes/cbc/nopadding"); cipher.init(cipher.encrypt_mode,keyspec,ivspec); byte[] encrypted = cipher.dofinal(toencrypt.getbytes()); return encrypted; } catch(exception e){ return null; // must return } }
for second 1 must catch exception encrypt method call, (also modify program logic stands):
public void actionperformed(actionevent e) . . . try { byte[] encrypted = encrypt(concaturl); string encryptedstring = bytestohex(encrypted); content.removeall(); content.add(new jlabel("concatenated user input -->" + concaturl)); content.add(encryptedtextfield); setcontentpane(content); } catch (exception exc) { // todo: handle exception } }
the lessons must learn this:
- a method return-type must always return object of type, mean in possible scenarios
- all checked exceptions must always handled
Comments
Post a Comment