The question is a bit general, so I'm not sure what you are really
asking.
To Debug a webpart to remove errors: In the Web.config of the site's web
application locate the following settings and change them as indicated
CallStack should be set equal to "true"
CustomErrors should be set equal to either "Off" or
"RemoteOnly"
These settings replace the non-descriptive SharePoint error message with
a Stack Dump and exception when a web part errors out. You can use this
info to fix the issue.
Otherwise, you handle errors in a web part just like you do in any other
code. Use Try/Catch blocks to intercept and handle the exceptions.
If the question was about something other than those two please provide
more detail.