我的问题是一个简单的nullpointerexception但是,我不明白我怎么弄清楚.这是代码.
我正在尝试运行primefaces的chat utilitie.我正在使用tomcat 7 ..据我所知,但现在我遇到了这个问题.
public class ChatView {
private final PushContext pushContext = PushContextFactory.getDefault().getPushContext();
private ChatUsers users;
private String privateMessage;
private String globalMessage;
private String username;
private boolean loggedIn;
private String privateUser;
private final static String CHANNEL = "/chat/";
public void setUsers(ChatUsers users) {
this.users = users;
}
public String getPrivateUser() {
return privateUser;
}
public void setPrivateUser(String privateUser) {
this.privateUser = privateUser;
}
public String getGlobalMessage() {
return globalMessage;
}
public void setGlobalMessage(String globalMessage) {
this.globalMessage = globalMessage; …Run Code Online (Sandbox Code Playgroud) 所以我有一个简单的表单,用户可以输入double值.

这就是我的表格应该是什么样子.
但是,如果用户有意或无意地输入非数字值,则会给我一个错误:
Input String was not correct format
Run Code Online (Sandbox Code Playgroud)
我听说过这样做的方法,称为异常处理.如何制作我自己的错误消息,以便程序不会崩溃?
我正在尝试调试一段生产代码。不是我写的,所以请不要批评它。我知道出于多种原因,这是一种糟糕的做法,如果可以,我会改变它,但我不能。
代码如下:
try
{
...
// Multiple lines of code that can throw exceptions
...
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
在这些多行代码中没有任何地方是手动抛出的异常。
尽管如此,以下是我尝试调试的情况下打印的全部内容:
Exception: -1
Run Code Online (Sandbox Code Playgroud)
Throwable.getMessage() 的 Java 文档说
getMessage
public String getMessage()
Returns the detail message string of this throwable.
Returns:
the detail message string of this Throwable instance (which may be null).
Run Code Online (Sandbox Code Playgroud)
所有可以抛出异常的非本地库方法都在更大的 try-catch 块中被调用的地方正确捕获。那么,特别是关于标准 JDK 中的异常,是否有任何可能的异常消息只是“-1”?
我的编译器给出了以下代码的错误:
#include <iostream>
#include <stdexcept>
using namespace std;
void test()
{
throw runtime_error("Error");
}
int main()
{
try
{
test();
}
for (int i = 0; i < 10; i++)
{
}
catch (exception& e)
{
cout << e.what();
}
}
Run Code Online (Sandbox Code Playgroud)
它说"错误:预期'捕获'之前'('令牌',它指的是for循环初始化中的'(').
在try块之后我是否必须立即编写catch块?我认为如果在try块中抛出一个错误,程序将冒出来,直到它找到一个合适的catch块.为什么这不适用于此?
哪种方法更适合(根据良好的编程)生产,以便轻松调试错误.
LOG.error("Error message ",e);
Run Code Online (Sandbox Code Playgroud)
要么
LOG.error("Error message "+e);
Run Code Online (Sandbox Code Playgroud)
要么
LOG.error("Error message "+e.getMessage());
Run Code Online (Sandbox Code Playgroud) try
{
some code
}
catch()
{
some code
}
finally
{
some code
}
try
{
some code
}
catch()
{
some code
}
finally
{
some code
}
Run Code Online (Sandbox Code Playgroud)
我知道如果在第一个try块中抛出异常,那么将执行第一个finally块.第二个最终块怎么样?
此外,如果要在出现异常时向用户显示消息,那么您应该在何处编写该消息,以及如何显示该消息?
仅供参考,我最近在一次采访中被问到了这些问题,并且感到难过.
为什么我不能抓住这个例外?
我的(客户端)代码:
Eigen::MatrixXd FFs ;
try
{
FFs.resize( NUMPATCHES, NUMPATCHES ) ;
}
catch( int e )
{
error( "Not enough memory :(" ) ;
return ;
}
Run Code Online (Sandbox Code Playgroud)
抛出异常的特征代码是向下几级.
EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
{
internal::check_rows_cols_for_overflow(rows, cols);
m_storage.resize(rows*cols, rows, cols);
}
哪个电话
void resize(DenseIndex size, DenseIndex rows, DenseIndex cols)
{
if(size != m_rows*m_cols)
{
internal::conditional_aligned_delete_auto(m_data, m_rows*m_cols);
if (size)
m_data = internal::conditional_aligned_new_auto(size);
else
m_data = 0;
EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
}
m_rows = rows;
m_cols = cols;
}
粗体线是在线前被击中的线:
throw std::bad_alloc();
Run Code Online (Sandbox Code Playgroud)
被击中,这发生在internal::conditional_aligned_delete_auto(m_data, …
我想问一下,如果发生异常,则在文本字段中显示某个字符串.当我尝试使用try时,捕获IOException,它给了我一个错误,它不能在try和同一个body中有一个catch.这可能应该在动作执行的方法中完成.
GUI:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display = new JTextField(20);
doMath math = new doMath();
String s = "";
String b= "";
//int counter;
JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;
JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
JButton Space;
public GUI()
{
super("Calculator");
setSize(400,400); …Run Code Online (Sandbox Code Playgroud) http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm#exception_common_methods 此站点显示包含了getLineNumber,但我无法使用它.谢谢
(我试图找到一个空指针异常)
我有很多自定义异常,我在代码中的特定情况下抛出,我想在方法的底部有一个catch块来处理它们.
所有异常都是Exception类CribbageException的子代,所以我想要:
public void myMethod(){
if (whatever){
throw new CardException();
}
if (something else){
throw new InvalidCardException();
}
if (scenario 3){
throw new TwoCardsException();
}
catch (CribbageException e) {
System.out.println(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个没有尝试错误的捕获.
有没有办法使用这种类型的异常处理?
我浏览了http://en.cppreference.com/w/cpp/error/exception和http://en.cppreference.com/w/cpp/io/basic_fstream,寻找可能引发的I/O异常.我知道我可以创建一个自定义异常类,并且有很多网站提供示例代码,但我想要一些专门针对最佳实践的指导.
还有,我应该集中精力使用图书馆让我的生活更轻松吗?
如果存在nil,强制解包会导致应用程序崩溃.这在您的应用程序的开发阶段非常酷.但是如果你懒得去做,那么这对于你的生产构建尤其令人头疼.
有没有人尝试过任何运算符重载/覆盖,以阻止生成构建的这些崩溃?