peter m hayward wrote:. . . how to add a try catch block around a component such as a text field or combobox . . .
That doesn't make sense. You do not apply try‑catch to a component, but to lines of code inside a method.
Are you referring to initialising a field in a constructor or initialiser block?
Whichever code you have, you can note that it might suffer an error leading to an exception (small e) being thrown by surrounding it with
try {...}, and you then have two options (which can be combined if necessary). You can handle the exception in a
catch clause or you can instruct code to run irrespective of an exception with a
finally clause. If you aren't handling the exception here and now, you can allow it to propagate (unchecked exceptions) or you can notify whichever code called the current code by declaring the exception (checked exceptions) with a
throws XYZException declaration. Remind yourself of the details in the
Java™ Tutorials. Regard a
throws declaration as meaning, “This method or constructor might suffer an XYZException, but we don't think this is the right place to handle it if it occurs, and any users of this code must work out how to handle it for themselves.”
Now, you appear to be doing exactly that in the code you posted. Obviously your
IDE defaults to writing a logging statement in its
catches. You can of course change that; there is probably a project configuration file which contains your options and there will be a menu to change those options and you can specify a different default contents for a
catch. What is happening is that your method call (line 4) declares that it might throw an SQLException and you have to handle that in lines 5‑7. If you change line 6 to something like
ex.printStackTrace();, you will get different behaviour from that code block.
Now, you can declare Exceptions (large E) from methods and constructors. If you suffer an exception which isn't handled from an initialiser block, you may find the JVM changes it to
this sort of exception, which isn't normally caught. I don't know whether that is what you meant to ask.
I am not convinced that you have arrived in the right forum: let's move you to where we usually discuss things “basic”.